rad 6.2.0
Loading...
Searching...
No Matches
Public Member Functions | Protected Member Functions | List of all members
rad::ThreadActivity Class Referenceabstract

#include <activity.hpp>

Inheritance diagram for rad::ThreadActivity:
rad::ThreadBaseActivity scxml4cpp::Activity MyTestDoActivity exmalserver::ActivityMoving rad::ActivityUpdateOldb rad::ConfigurableActivity server::ActivityMoving exciiserver::ActivityMoving exciiserver::ActivityMovingDds rad::utest::Activity

Public Member Functions

 ThreadActivity (const ThreadActivity &)=delete
 
ThreadActivityoperator= (const ThreadActivity &)=delete
 
virtual void Run ()=0
 
pthread_t GetNativeHandle () const override
 
bool IsThreadJoined () const override
 
 ThreadBaseActivity (ThreadBaseActivity const &)=delete
 
- Public Member Functions inherited from rad::ThreadBaseActivity
 ThreadBaseActivity (ThreadBaseActivity const &)=delete
 
void start (scxml4cpp::Context *c) override
 
void stop (scxml4cpp::Context *c) override
 
bool IsStopRequested () const
 
- Public Member Functions inherited from scxml4cpp::Activity
 Activity (const std::string &id)
 
virtual ~Activity ()
 
std::string getId () const
 
void setId (const std::string &id)
 

Protected Member Functions

void StartThread () override
 
void JoinThread () override
 
std::string GetPthreadName ()
 
int SetPthreadName (const std::string &name)
 

Detailed Description

This class is the base class for thread-based do-activities.

User should use IsStopRequested() to check for when to stop the activity.

Example implementation:

 class MyActivity : public ThreadActivity {
     void Run() override {
         while(!IsStopRequested()) {
             // do work
             ...
         }
     }
 };
Attention
methods of this class are not thread safe. It is assumed that the activity thread attributes cannot be modified by other threads. If thread safety is needed, the developer will subclass and add protection.

Constructor & Destructor Documentation

◆ ThreadActivity()

rad::ThreadActivity::ThreadActivity ( const ThreadActivity & )
delete

Member Function Documentation

◆ GetNativeHandle()

pthread_t rad::ThreadActivity::GetNativeHandle ( ) const
overridevirtual
Returns
A handle to the native thread implementation.

TODO review whether 'const' is needed.

Implements rad::ThreadBaseActivity.

◆ GetPthreadName()

std::string rad::ThreadActivity::GetPthreadName ( )
protected

This method is similar to GetName() but it uses the platform specific pthread_self() function instead of the platform independent native_handle().

This avoid data race conditions between the main thread that started the activity and the activity thread itself that tries to get/set its name. In fact when the activity is started the m_thread attribute is assigned via a swap of the std::thread. This std::thread seems to be read also by native_handle() creating a possible data race condition.

Returns
The thread name.

◆ IsThreadJoined()

bool rad::ThreadActivity::IsThreadJoined ( ) const
overridevirtual
Returns
true if the thread is not running, false otherwise.

Implements rad::ThreadBaseActivity.

◆ JoinThread()

void rad::ThreadActivity::JoinThread ( )
overrideprotectedvirtual

Helper method to stop a do-activity thread.

Implements rad::ThreadBaseActivity.

◆ operator=()

ThreadActivity & rad::ThreadActivity::operator= ( const ThreadActivity & )
delete

◆ Run()

virtual void rad::ThreadActivity::Run ( )
pure virtual

Interface to be implemented by child classes to implement the activity thread.

Implemented in rad::utest::Activity, exciiserver::ActivityMoving, exciiserver::ActivityMovingDds, exmalserver::ActivityMoving, server::ActivityMoving, rad::ActivityUpdateOldb, and MyTestDoActivity.

◆ SetPthreadName()

int rad::ThreadActivity::SetPthreadName ( const std::string & name)
protected

This method is similar to SetName() but it uses the platform specific pthread_self() function instead of the platform independent native_handle().

See GetPthreadName() for a full explanation of the advantages vs SetName().

Parameters
nameName for the thread.
Returns
Error code from pthread_setname_np() function.

◆ StartThread()

void rad::ThreadActivity::StartThread ( )
overrideprotectedvirtual

Helper method to start a do-activity thread.

Implements rad::ThreadBaseActivity.

◆ ThreadBaseActivity()

rad::ThreadBaseActivity::ThreadBaseActivity ( ThreadBaseActivity const & )
delete

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