### Create and Delete Threads (Windows)

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

- _beginthread and _endthread from the multithread libraries
- CreateThread and ExitThread from the Windows API
- IThread from the User Interface classes

_beginthread and _endthread
You should use _beginthread to create any 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.

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.

CreateThread and ExitThread
Threads created by the CreateThread API do not have access to the resource management facilities or to VisualAge for C++ 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 ExitThread 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 additional threads. This member function has three overloaded versions and three corresponding constructors for:

- Functions compatible with _beginthread; that is, the functions that have _Optlink linkage, take one argument of void* type, and return void.
- Functions compatible with CreateThread; that is, the functions that have __stdcall linkage, take one argument of unsigned long type, and return void.
- Other functions.

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. It does not only what other mechanisms do, it does it better. Unlike _beginthread, you don't have to explicitly call _endthread to clean up the environment, and unlike CreateThread, you do not have to write your own exception handler.

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

- Set thread priority
- Set thread attributes
- Do a reference count for objects dispatched on a thread so they are automatically deleted when the thread ends
- Dispatch a member function of a C++ object on a separate thread
- Control other aspects of your threads

Multithreaded Applications
Thread Local Storage

Use the Multithread Libraries
Terminate Execution with Multiple Threads

Signal and Exception Handlers in Multithreaded Applications