CORBA 2.6 Programmer's Guide for C++

specifies the thread function that will be called when the thread is launched (the main line of the new thread). The third
parameter is the thread argument referenced in Step 2, which allows the Fw_Thread::create function to pass context to
the thread. (The third parameter could be the C++ keyword this or a reference to any other object.) The last parameter sets
the relative priority of the thread. The possible priorities are Fw_Thread::Default_Priority,
Fw_Thread::Higher_Priority, and Fw_Thread::Lower_Priority.
Using the yield(), join() and cancel() Methods
Multithreading on NonStop systems is non-preemptive; that is, each thread executes until it relinquishes control. Control is
relinquished when the thread waits on a mutex or condition variable, issues a remote procedure call (such as a CORBA method
invocation), or explicitly calls the yield() method. You can also use the join() and cancel() methods to manage thread
operation.
Invoke the yield() method to place the current thread at the end of the ready list and dispatch the next ready thread:
Fw_Thread::yield();
Calls to yield() can be useful to increase scheduling equity; however, such calls must be used judiciously. Misuse causes
unnecessary context switching, which increases overhead without increasing equity. For example, it is counterproductive for a thread
to yield control while it has a needed resource locked.
Invoke the join() method to block the current thread until another thread, specified in the first parameter to the call, completes.
This method is useful when a thread requires work to be done by several other threads in parallel. The join() method is invoked as
follows:
Fw_Thread::join (lp_threadid, &lp_result);
The result from the second thread is passed back to the caller in the second parameter. The method returns 0 if the call was
successful.
Invoke the cancel() method to cancel operation of another thread, as follows:
Fw_Thread::cancel (oldthrd_threadid);
Creating and Using a Mutex
Use a mutex (short for mutual exclusion) to ensure that a critical sequence of code will run without interruption by other threads. For
example, you would typically use a mutex to protect access to critical data shared by multiple threads, ensuring that only one thread
can modify the data at a time. Code protected by a mutex or a similar mechanism is referred to as thread-safe (reentrant) code.
To create and use a mutex, define an instance of the Fw_Thread::Mutex 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 mutex:
Fw_Thread::Mutex appMutex;
2.
Enclose the critical section of codefor example, the portion of code that modifies critical datawith calls to lock() and
unlock():
appMutex.lock();
//Code to be protected goes here
appMutex.unlock();
3.
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.