This class provides a mechanism to run two threads alternately. More...
#include <AlternateThreadPT.hxx>
Public Types | |
enum | ThreadStatus { UNEXISTING, NORMAL_CYCLE, TERMINATION_REQUESTED, READY_TO_JOIN } |
Public Member Functions | |
AlternateThreadPT () | |
virtual | ~AlternateThreadPT () |
void | start () |
Create and launch the slave thread. More... | |
virtual void | signalSlaveAndWait () |
Block the master thread and release the slave thread. More... | |
void | terminateSlaveThread () |
Terminate the slave thread. More... | |
virtual void | signalMasterAndWait () |
Block the slave thread and release the master thread. More... | |
bool | isTerminationRequested () const |
Return true if the master requested the slave thread termination. More... | |
ThreadStatus | getThreadStatus () const |
Return the thread status. More... | |
Protected Member Functions | |
virtual void | run ()=0 |
This method must be implemented in subclasses and will be run in the slave thread. More... | |
Private Member Functions | |
void | signalAndWait () |
Static Private Member Functions | |
static void * | runThread (void *instance) |
static void | threadCleanupFct (void *instance) |
Private Attributes | |
pthread_t | _threadId |
ThreadStatus | _threadStatus |
pthread_cond_t | _pingPongCond |
pthread_mutex_t | _pingPongMutex |
This class provides a mechanism to run two threads alternately.
Alternate threads can be necessary when two pieces of code must run alternately and communicate but are difficult or impossible to synchronize explicitly (e.g. asynchronous algorithms in optimizer loop). This class guarantees that the two threads NEVER run concurrently, so no lock mechanism is necessary when sharing data between them.
The two threads are called "master thread" and "slave thread". The master thread is the one that calls the method start() and that will continue to run after the destruction of this object. The slave thread is created when the method start() is called. It will run the code in the method run() and will be destroyed when the master thread calls terminateSlaveThread() or at the destruction of this object.
When the master thread calls start(), it will block and the slave thread will begin to execute the code in the method run(). The two threads can then alternate by calling the method signalMasterAndWait() (in the slave thread) and signalSlaveAndWait() (in the master thread). Finally, the master thread must call terminateSlaveThread() to terminate the slave thread. There is no explicit mechanism for the slave thread to request its own termination, but this can be done with an external flag (see the pool object in OptimizerLoop for instance).
This class is purely virtual. Subclasses must implement the run() method that will be executed in the slave thread.
Definition at line 55 of file AlternateThreadPT.hxx.
Enumerator | |
---|---|
UNEXISTING | |
NORMAL_CYCLE | |
TERMINATION_REQUESTED | |
READY_TO_JOIN |
Definition at line 58 of file AlternateThreadPT.hxx.
AlternateThreadPT::AlternateThreadPT | ( | ) |
Definition at line 31 of file AlternateThreadPT.cxx.
References _pingPongCond, _pingPongMutex, and YASSERT.
|
virtual |
Definition at line 38 of file AlternateThreadPT.cxx.
References _pingPongCond, _pingPongMutex, terminateSlaveThread(), and YASSERT.
AlternateThreadPT::ThreadStatus AlternateThreadPT::getThreadStatus | ( | ) | const |
Return the thread status.
Definition at line 134 of file AlternateThreadPT.cxx.
References _threadStatus.
bool AlternateThreadPT::isTerminationRequested | ( | ) | const |
Return true if the master requested the slave thread termination.
Definition at line 129 of file AlternateThreadPT.cxx.
References _threadStatus, and TERMINATION_REQUESTED.
|
protectedpure virtual |
This method must be implemented in subclasses and will be run in the slave thread.
The slave thread must call signalMasterAndWait() when necessary to give the control back to the master thread. When returning from this method, the slave thread MUST check for an eventual termination request (with the method isTerminationRequested()). If the termination is requested, the slave thread must perform any necessary cleanup and finish as soon as possible.
Referenced by runThread().
|
staticprivate |
Definition at line 139 of file AlternateThreadPT.cxx.
References _pingPongMutex, _threadStatus, DEBTRACE, NORMAL_CYCLE, run(), threadCleanupFct(), and YASSERT.
Referenced by start().
|
private |
Definition at line 122 of file AlternateThreadPT.cxx.
References _pingPongCond, _pingPongMutex, _threadStatus, NORMAL_CYCLE, TERMINATION_REQUESTED, and YASSERT.
Referenced by signalMasterAndWait(), and signalSlaveAndWait().
|
virtual |
Block the slave thread and release the master thread.
Definition at line 114 of file AlternateThreadPT.cxx.
References _threadId, DEBTRACE, signalAndWait(), and YASSERT.
|
virtual |
Block the master thread and release the slave thread.
Definition at line 106 of file AlternateThreadPT.cxx.
References _threadId, DEBTRACE, signalAndWait(), and YASSERT.
Referenced by terminateSlaveThread().
void AlternateThreadPT::start | ( | ) |
Create and launch the slave thread.
This method must not be called by the slave thread or if a slave thread is already running.
Definition at line 51 of file AlternateThreadPT.cxx.
References _pingPongCond, _pingPongMutex, _threadId, _threadStatus, DEBTRACE, runThread(), UNEXISTING, and YASSERT.
void AlternateThreadPT::terminateSlaveThread | ( | ) |
Terminate the slave thread.
Definition at line 64 of file AlternateThreadPT.cxx.
References _pingPongCond, _pingPongMutex, _threadId, _threadStatus, DEBTRACE, NORMAL_CYCLE, READY_TO_JOIN, signalSlaveAndWait(), TERMINATION_REQUESTED, UNEXISTING, and YASSERT.
Referenced by ~AlternateThreadPT().
|
staticprivate |
Definition at line 166 of file AlternateThreadPT.cxx.
References _pingPongCond, _pingPongMutex, _threadStatus, DEBTRACE, and READY_TO_JOIN.
Referenced by runThread().
|
private |
Definition at line 104 of file AlternateThreadPT.hxx.
Referenced by AlternateThreadPT(), signalAndWait(), start(), terminateSlaveThread(), threadCleanupFct(), and ~AlternateThreadPT().
|
private |
Definition at line 105 of file AlternateThreadPT.hxx.
Referenced by AlternateThreadPT(), runThread(), signalAndWait(), start(), terminateSlaveThread(), threadCleanupFct(), and ~AlternateThreadPT().
|
private |
Definition at line 102 of file AlternateThreadPT.hxx.
Referenced by signalMasterAndWait(), signalSlaveAndWait(), start(), and terminateSlaveThread().
|
private |
Definition at line 103 of file AlternateThreadPT.hxx.
Referenced by getThreadStatus(), isTerminationRequested(), runThread(), signalAndWait(), start(), terminateSlaveThread(), and threadCleanupFct().