CORBA 2.6.1 Programmer's Guide for C++

appMutex.lock();
//Code to be protected goes here
appMutex.unlock();
Alternatively, if needed, you can use the try_lock() method. This method attempts to lock the mutex and returns immediately with a nonzero
result if the attempt fails.
The class Fw_Thread::Preemption_Mutex is also included in $NSD_ROOT/include/nsdevent/vthread.h. This class, which wraps Fw_Thread::Mutex
with a consciousness of preemption, is a no-op on non-preemptive threading platforms such as NonStop systems. On preemptive threading
platforms (such as Unix and Microsoft Windows), this class behaves identically to
Fw_Thread::Mutex.
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:
1. Include the
$NSD_ROOT/include/nsdevent/vthread.h header file in your program:
#include <nsdevent/vthread.h>
2. Define a condition variable:
Fw_Thread::Condition_Variable workDone;
3. 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();
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.
Note:
In the above example, if Thread2 runs before Thread1 and invokes the signal ( ) member function, the wait ( )
member function in Thread1 returns without blocking, because the condition of the wait ( ) has already been satisfied
through the previous signal ( ). For more informaton about the behavior of Condtion_Variable class and its member
functions, see the HP NonStop CORBA 2.6.1 Programmer's Reference.
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.
To set the value of a 32–bit entry in the table, call the specific_set() method, passing the associated key returned by
specific_key_create().