Windows threading: _beginthread vs _beginthreadex vs CreateThread C++

后端 未结 17 1467
臣服心动
臣服心动 2020-11-22 17:26

What\'s a better way to start a thread, _beginthread, _beginthreadx or CreateThread?

I\'m trying to determine what are the adv

17条回答
  •  一整个雨季
    2020-11-22 17:50

    Compared to _beginthread, with _beginthreadex you can:

    1. Specify security attributes.
    2. Start a thread in suspended state.
    3. You can get the thread id which can be used with OpenThread.
    4. The thread handle returned is guaranteed to be valid if the call was successful. There for you need to close the handle with CloseHandle.
    5. The thread handle returned can be used with synchronization APIs.

    The _beginthreadex closely resembles CreateThread, but the former is a CRT implementation and the latter a Windows API call. The documentation for CreateThread contains the following recommendation:

    A thread in an executable that calls the C run-time library (CRT) should use the _beginthreadex and _endthreadex functions for thread management rather than CreateThread and ExitThread; this requires the use of the multi-threaded version of the CRT. If a thread created using CreateThread calls the CRT, the CRT may terminate the process in low-memory conditions.

提交回复
热议问题