Inter-thread communication and data sharing

From reSIProcate
Jump to: navigation, search

resiprocate is designed to be a threadsafe library. To accomplish this, all interthread communication and data sharing should be accomplished using the threadsafe Fifo classes (see the Fifo documentation for details). If you are looking to add functionality to the stack, and you are looking to share data between threads, *please* investigate using existing Fifos, or adding Fifos as need be.

There are a few exceptions to this rule, as of revision 4878 of /main. These are enumerated below.

The multithread data access and communication classes are: Mutex, Condition, and Lock. Although the classes ReadLock and WriteLock are defined in Lock, they are not (as of 4878) used anywhere.

As of revision 4878 of /main, here are the uses of these classes outside the Fifo classes, by class, in the resiprocate library (external applications such as dum are not included):


The counter uses a Mutex called the 'allocationMutex' to serialize accesses to the AllocationMap (using Lock, as are all of the follwing).


A Mutex called _mutex (poorly named) is used to serialize access to the logging level.


There is a mutex called mShutdownMutex used to serialize access to the mShutdown member variable of the class.
There is a Condition called mShutdownCondition which is signaled when mShutdown is set. This is for the implementation of the waitForShutdown(int ms) function.


There is a mutex called shutDownMutex used to serialize acces to mShuttingDown.
There is a mutex called mAppTimerMutex used to serialize access to mAppTimers.


There is a mutex called mMutex (poorly named) used to serialize access to the statistics data in the message in the functions 'loadIn' and 'loadOut'. These are only used within the StatisticsManager.


A mutex called mMutex (poorly named) is used to serialize access to the time and the count.

As you may have guessed, these classes are useful abstractions of multi-thread data access and data sharing concepts to support system independence between Win32 and POSIX based systems. If you want your application or resiprocate addition to be able to make use of those abstractions, look at the doxygen for the following classes:

Mutex: a semaphore that can be locked by only one thread at a time. Used to serialize access to a data member (with Lock)
Condition: a condition variable that can be signaled or waited on. Used for scheduling.
Lock: a convenience class to lock a Lockable object (such as a Mutex) on construction, and unlock it on destruction. Note that Lock is exception safe -- in general, do not use Mutex lock/unlock directly.

See the documentation for those Classes in doxygen for more information.