Multithreading is a programming facility that enables concurrent programming. A thread is a lightweight process that coexists with other threads in the same address space. Each thread has its own set of CPU registers and its own stack. It shares the process instruction space with other threads. Having multiple threads in a process means that there are many "simultaneous" points of execution. Although NonStop DOM application programmers are generally unaware of it, the NonStop DOM ORB uses threading internally. A NonStop DOM application programmer may also decide to use explicit threading.
The main purpose of threads is to increase the throughput of a process by using parallelism. Some applications lend themselves better than others to be implemented in a multithreaded fashion. In a distributed NonStop DOM application, you can use threads in the client, the server, or both.
Threading can be useful when:
One typical use of threading in NonStop DOM client programs is to issue concurrent requests to one or more objects. When threading is not used, method requests are synchronous. That is, when a client issues a method request to an object, it must wait for the response to arrive before doing any further processing. If a client application creates multiple threads, each thread can issue method requests and thus have concurrent outstanding requests.
The sample program $NSD_ROOT/samples/bank/feed contains a multithreaded NonStop DOM client program.
Note: You can use the dynamic invocation interface instead of threading to issue asynchronous requests. Depending on the situation, this may be a desirable alternative to threading. Be aware, that using the dynamic invocation interface generally requires more programming effort.
If you use the default POA threading policy, no thread programming is required. The POA will spawn additional threads as necessary to handle additional requests. The POA initially creates a single thread to handle requests for objects in a server. If a new request arrives before an outstanding request has completed, the POA may spawn another thread to handle the new request. For this to occur, the thread processing the original request must have become blocked or have explicitly released control of the processor. If the thread does not release control of the processor, the POA thread cannot run and spawn a new thread.
In certain situations, you may find it desirable to make a server spawn new threads. Such threads can coexist with POA-spawned threads.
Keep the following points in mind when dealing with multithreaded servers:
NonStop DOM provides an object-oriented, portable application programming interface (API) to threads. With this programming interface, you can perform the following thread operations:
Also provided are two concurrency control mechanisms: mutexes and condition variables. For mutexes, you may perform the following operations:
For condition variables, you may perform the following operations:
The header file for these classes is vthread.h. Refer to the Reference Guide for a detailed description of the NonStop DOM threading interface.
In the OSS environment, this API is built on the DCE Threads package. It is included in the NonStop DOM SRL, so you need not (and should not) link a separate threads library with your threaded application programs. See the Tandem manual NonStop DCE Application Programming Guide for detailed information about using the threads API. Although not recommended, you can use the underlying threading implementation for the specific platform. For example, you could use the DCE Threads threading interface directly.
Note: If you have the DCE product installed, do not use the libraries provided by DCE. The NonStop DOM SRL provides the threads API and the supported jacket routines.