Create and Delete Threads (OS/2)

The mechanisms for creating and deleting threads with IBM C and C++ Compilers are:

_beginthread and _endthread
The multithread libraries provide these two functions to create new threads and to end them. You should use _beginthread to create any new threads that call IBM C and C++ Compilers library functions. When the thread is started, the library environment is initialized and ensures that resources and data are handled correctly between threads.

IBM C and C++ Compilers does not limit the number of threads you can create, but the OS/2 operating system does. The IBM C and C++ Compilers product also provides the global variable _threadid that identifies your current thread, and the function _threadstore that gives you a private thread pointer to which you can assign any thread-specific data structure.

DosCreateThread and DosExit
You can also create threads with DosCreateThread. The function that is to run on the thread created by DosCreateThread must have _System linkage. If you need to start a new thread for a function with any other type of linkage, you cannot use DosCreateThread.

Threads created by DosCreateThread do not have access to the resource management facilities or to IBM C and C++ Compilers exception handling. You must use a #pragma handler directive for the thread function to ensure correct exception handling. You should also call _fpreset from the new thread to ensure the floating-point control word is set correctly for the thread. Although you can use DosExit to end threads created with _beginthread, you should use _endthread to ensure that the necessary cleanup of the environment is done.

IThread
The start member function of the IThread class is used to start new threads. This member function has three overloaded versions and three corresponding constructors for:

Because IThread can handle functions that fall under both static and dynamic thread control mechanisms, and those that do not, it is the preferred thread handling mechanism. Unlike _beginthread, you do not have to explicitly call _endthread to clean up the environment, and unlike DosCreateThread, you do not have to write your own exception handler.

You can use the IThread class in your multithread programs to accomplish several tasks:



Multithreaded Applications


Use the Multithread Libraries
Terminate Execution with Multiple Threads