SmartSoft Component-Developer API
Public Member Functions | Protected Member Functions | List of all members
Smart::ITask Class Referenceabstract

Encapsulates a user-level thread. More...

#include <smartITask.h>

Inheritance diagram for Smart::ITask:
Smart::IShutdownObserver Smart::IActiveQueryServerHandler< RequestType, AnswerType > Smart::IManagedTask

Public Member Functions

 ITask (IComponent *component=nullptr)
 Default constructor. More...
 
virtual ~ITask ()=default
 Default destructor. More...
 
virtual int start ()=0
 Creates and starts a new thread (if not yet started) More...
 
virtual int stop (const bool wait_till_stopped=true)=0
 Stops the currently active thread (if it was started before) More...
 
- Public Member Functions inherited from Smart::IShutdownObserver
 IShutdownObserver (ShutdownSubject *subject=nullptr)
 The default constructor. More...
 
virtual ~IShutdownObserver ()
 The default destructor. More...
 

Protected Member Functions

virtual void on_shutdown ()
 Default implementation of the IShutdownObserver interface. More...
 
virtual bool test_canceled ()=0
 Tests whether the thread has been signaled to stop. More...
 
virtual void sleep_for (const Duration &duration)=0
 Blocks execution of the calling thread during the span of time specified by rel_time. More...
 
virtual int task_execution ()=0
 Method which runs in a separate thread if activated. More...
 
- Protected Member Functions inherited from Smart::IShutdownObserver
void attach_self_to (ShutdownSubject *subject)
 call this method from within the constructor of derived classes More...
 
void detach_self_from (ShutdownSubject *subject)
 this method is called within the destructor More...
 

Detailed Description

Encapsulates a user-level thread.

ITasks should be used to implement user-level threads within a component. ITasks are managed by the provided IComponent instance. For example, during component shutdown, the ITask are automatically triggered to stop(). In this way, user-threads can properly shutdown and clean-up their local resources such as e.g. closing internally used device drivers. Please overload the on_shutdown() upcall method in case you need to perform individual cleanup procedures.

Constructor & Destructor Documentation

◆ ITask()

Smart::ITask::ITask ( IComponent component = nullptr)
inline

Default constructor.

◆ ~ITask()

virtual Smart::ITask::~ITask ( )
virtualdefault

Default destructor.

Member Function Documentation

◆ on_shutdown()

virtual void Smart::ITask::on_shutdown ( )
inlineprotectedvirtual

Default implementation of the IShutdownObserver interface.

The default shutdown procedure is to call the stop() method which triggers the thread to stop and awaits until it is closed using the internal join method.

Implements Smart::IShutdownObserver.

Reimplemented in Smart::IManagedTask.

◆ sleep_for()

virtual void Smart::ITask::sleep_for ( const Duration duration)
protectedpure virtual

Blocks execution of the calling thread during the span of time specified by rel_time.

Thread-sleeping is sometimes platform-specific. This method encapsulates the blocking sleep. Calling this method blocks the execution of the calling thread for a time specified by rel_time.

Parameters
durationrelative time duration for the thread to sleep

◆ start()

virtual int Smart::ITask::start ( )
pure virtual

Creates and starts a new thread (if not yet started)

A new thread is spawned if no thread has been spawned yet before for this instance.

Returns
0 on success (and if thread has already been started) or -1 on failure

◆ stop()

virtual int Smart::ITask::stop ( const bool  wait_till_stopped = true)
pure virtual

Stops the currently active thread (if it was started before)

The internal thread is signaled to stop. If wait_till_stopped is set to true then the call to this method blocks until the internal thread has actually exited (typically using thread.join() internally).

Parameters
wait_till_stoppedwaits until the thread has been exited
Returns
0 on success or -1 on failure

◆ task_execution()

virtual int Smart::ITask::task_execution ( )
protectedpure virtual

Method which runs in a separate thread if activated.

The task_execution() method has to be provided (i.e. overloaded) by the user and it implements the activity of the task object.

Returns
0 for all OK or -1 otherwise

Implemented in Smart::IManagedTask.

◆ test_canceled()

virtual bool Smart::ITask::test_canceled ( )
protectedpure virtual

Tests whether the thread has been signaled to stop.

This method allows to implement cooperative thread stopping.

Returns
true if stop was called or false otherwise.

The documentation for this class was generated from the following file: