Multithread Programs
Go Up to C Run-Time Library Reference
Win32 programs can create more than one thread of execution. If your program creates multiple threads, and these threads also use the C++ runtime library, you must use the CW32MT.LIB or CW32MTI.LIB library instead.
Note: The multithreaded runtime is the only available option for the Modern Toolchain. The choice to link a different runtime based on threading is not applicable for applications using the Modern toolchain.
The multithread libraries provide the following functions which you use to create threads:
The multithread libraries also provide the following corresponding functions that terminate threads:
- _endthread
- _endthreadex
- _threadid is a global variable that contains the current identification number of the thread, also known as the thread ID).
The header file stddef.h contains the declaration of _threadid.
When you compile or link a program that uses multiple threads, you must use the -tWM
compiler switch. For example:
BCC32 -tWM THREAD.C
Note: Take special care when using the signal function in a multithread program. The
SIGINT
, SIGTERM
, and SIGBREAK
signals can be used only by the main thread (thread one) in a non-Win32 application. When one of these signals occurs, the currently executing thread is suspended, and control transfers to the signal handler (if any) set up by thread one. Other signals can be handled by any thread. A signal handler should not use C++ runtime library functions, because a semaphore deadlock might occur. Instead, the handler should simply set a flag or post a semaphore, and return immediately.