CORBA 2.6.1 Programmer's Guide for C++

subdirectory. Refer to the NonStop CORBA 2.6.1 Programmer's Reference for a detailed description of these classes and their methods.
HP recommends that NonStop CORBA applications use the vthread API for multithreading because it is object-oriented and easier to use than
the POSIX threading (pthread) API. However, if you require conformance to POSIX threading standards, you can use the underlying threading
implementation for the specific platform. In the OSS environment, the vthread API is built on the Standard POSIX Thread pthread package
(T1248), which is included in the
$NSD_ROOT/include directory. For detailed information about using the pthread API, refer to the Open System
Services Programmer's Guide (Section 11).
Caution:
If you use the pthreads API directly you need to change your CORBA applications to use the new APIs
associated with pthreads (product number T1248).
Creating and Using Threads
To create and use threads through the vthread API, perform the following steps:
1. Include the $NSD_ROOT/include/nsdevent/vthread.h header file in your program:
#include <nsdevent/vthread.h>
2. Create a function that will be the body of a thread in the global area of your application; for example:
void my_function (void* threadArg);
This function cannot be a class instance method; otherwise a compiler error occurs. In addition, the function must have the signature
expected by the thread-generating function described in Step 3. The
threadArg parameter is needed for communication between the
thread function and the thread-generating function.
3. Create a member function that invokes a thread-generating function that uses the thread function created in Step 2:
Fw_Thread::Id_ptr threadId =
Fw_Thread::create ("My Thread", (Fw_Thread::Function) my_function, void* param3,
Fw_Thread::Default_Priority);
The first parameter of the Fw_Thread::create function is a string defining the name of the thread. The second parameter 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:
1. Include the
$NSD_ROOT/include/nsdevent/vthread.h header file in your program:
#include <nsdevent/vthread.h>
2. Define a mutex:
Fw_Thread::Mutex appMutex;
3. Enclose the critical section of code—for example, the portion of code that modifies critical data—with calls to lock() and unlock():