CORBA 2.6 Programmer's Guide for C++

Creating and Using a Condition Variable
A condition variable communicates information about shared data. For example, you can use a condition variable to signal that a
queue is no longer empty, or that it has become empty, or that something else needs to be done or can be done within the shared data
manipulated by threads in an application program.
A condition variable allows a thread to start a task and then enter a waiting state until a necessary condition (performed by another
thread) is satisfied. Another thread must be written to signal the waiting thread when the condition is satisfied.
To create and use a condition variable, define an instance of the Fw_Thread::Condition_Variable class and perform the
following steps:
Include the $NSD_ROOT/include/nsdevent/vthread.h header file in your program:
#include <nsdevent/vthread.h>
1.
Define a condition variable:
Fw_Thread::Condition_Variable workDone;
2.
Enclose each critical section of code with calls to the lock() and unlock() member functions of the condition variable.
Place either the wait() or the signal() member function of the condition variable between the lock() and unlock()
function calls.
Thread 1:
appCondV.lock();
//Code to be protected
appCondV.wait();
//Code to be protected
appCondV.unlock();
Thread 2:
appCondV.lock();
//Code to be protected
appCondV.signal();
//Code to be protected
appCondV.unlock();
3.
As shown in the vthread.h file, the vthread implementation of a condition variable inherits from a mutex. Each time a vthread
Condition_Variable class is instantiated, a mutex is created and associated with a condition variable in the underlying pthread
interface. Therefore, invoking the lock() method of the Condition_Variable class is equivalent to locking the underlying
mutex. As a result, only one thread can own a condition variable at a given time. To allow other threads to compete for the condition
variable, the current owner of the condition variable must unlock the associated mutex.
Since a mutex is built into a condition variable, you do not need to declare a separate mutex for the condition variable. All you need
to do is to enclose the critical section of code with the lock() and unlock() member functions of the condition variable and use
the wait() and signal() member functions as indicated above.
If needed, you can use the broadcast() method in place of the signal() method. The broadcast() method signals all
threads that are waiting on this condition variable.
Saving and Retrieving Thread-Specific Data
The methods specific_key_create(), specific_set(), and specific_get() allow you to save and retrieve data
specific to a particular thread.
The vthread API provides each thread with a table of thirty-two 32-bit words to store data specific to that thread. Each word can be
used as a pointer to a larger set of data stored elsewhere. This data can then be shared by various application modules associated with
the thread.
To use this feature, do the following within a thread:
Call specific_key_create(). This method returns a key to an entry within the data table for the current thread, and also
allows you to identify the destructor function to be used to clean up data for that key.