C Run-Time Library Functions for Thread Control

All Win32 programs have at least one thread. Any thread can create additional threads. A thread can complete its work quickly and then terminate, or it can stay active for the life of the program.

The LIBCMT and MSVCRT C run-time libraries provide the following functions for thread creation and termination: _beginthread, _beginthreadex and _endthread, _endthreadex.

The _beginthread and _beginthreadex functions create a new thread and return a thread identifier if the operation is successful. The thread terminates automatically if it completes execution, or it can terminate itself with a call to _endthread or _endthreadex.

Note

If you are going to call C run-time routines from a program built with Libcmt.lib, you must start your threads with the _beginthread or _beginthreadex function. Do not use the Win32 functions ExitThread and CreateThread. Using SuspendThread can lead to a deadlock when more than one thread is blocked waiting for the suspended thread to complete its access to a C run-time data structure.

The _beginthread and _beginthreadex Functions

The _beginthread and _beginthreadex functions create a new thread. A thread shares the code and data segments of a process with other threads in the process but has its own unique register values, stack space, and current instruction address. The system gives CPU time to each thread, so that all threads in a process can execute concurrently.

_beginthread and _beginthreadex are similar to the CreateThread function in the Win32 API but has these differences:

  • They initialize certain C run-time library variables. This is important only if you use the C run-time library in your threads.

  • CreateThread helps provide control over security attributes. You can use this function to start a thread in a suspended state.

_beginthread and _beginthreadex return a handle to the new thread if successful or an error code if there was an error.

The _endthread and _endthreadex Functions

The _endthread function terminates a thread created by _beginthread (and similarly, _endthreadex terminates a thread created by _beginthreadex). Threads terminate automatically when they finish. _endthread and _endthreadex are useful for conditional termination from within a thread. A thread dedicated to communications processing, for example, can quit if it is unable to get control of the communications port.

See Also

Concepts

Multithreading with C and Win32