Why not use CreateThread instead of using _beginthreadex

Source: Internet
Author: User

When writing C + + code, keep in mind a word: should never call CreateThread. Instead, you should use the Visual C + + Run-time library function _beginthreadex.
As if the CreateThread function is a tiger, why does Microsoft want to develop this function?
From the relevant information found on the Internet, now a summary of the relevant staff to thank.
From "Windows core Programming":
The CreateThread function is the Windows function used to create a thread. However, if you are writing C + + code, you should never call CreateThread. Instead, you should use the Visual C + + Run-time library function _beginthreadex. If you do not use Microsoft's Visual C + + compiler, your compiler vendor has its own createthred substitution function.
To enable multithreaded C and C + + programs to run correctly, you must create a data structure and associate it with each thread that uses the C + + run-time library function. When you call the C + + Run-time Library, these functions must know to view the data blocks of the calling thread, so that they do not adversely affect other threads.
1. Each thread obtains its own tiddata memory structure that is allocated by the stack of the C + + Run-time library.
2. The address of the thread function passed to _beginthreadex is saved in the Tiddata memory block. The arguments passed to the function are also saved in the data block.
3._beginthreadex does call CreateThread from within, because this is the only way the operating system knows how to create a new thread.
4. When calling Createtthread, it is told to start executing a new thread by calling _threadstartex instead of PFNSTARTADDR. Also, the arguments passed to the thread function are the TIDDATA structure rather than the Pvparam address.
5. If all goes well, it will return the thread handle like CreateThread. If any operation fails, it returns NULL.
The difference between _beginthreadex and _beginthread functions. The _beginthread function has fewer parameters, so the _beginthreadex function is more restrictive than the comprehensive feature.
For example, if you use _beginthread, you cannot create a new thread with security attributes, you cannot create a paused thread, and you cannot obtain the thread's ID value.
Holly ():
Oldworm provides examples of good use, and also uses compiler control.
To explain the theoretical difference:
CreateThread, _beginthread and _beginthreadex are all used to start the thread, but we see that Oldworm did not provide the _beginthread way, the exam, the big hint Beginthread is _ Beginthreadex a subset of functions, although _beginthread is called _beginthreadex but he masks features such as security features, so _beginthread and CreateThread are not the same level, _ Beginthreadex and CreateThread are completely replaceable in function, so let's compare _beginthreadex and createthread!
The CRT's function library existed before the thread appeared, so the original CRT could not really support the threads, which led us to choose the CRT library when we were programming, as we looked up the functions of the CRT in MSDN:
Libraries
LIBC. LIB single thread Static library, retail version
LIBCMT. LIB multithread Static Library, retail version
MSVCRT. LIB Import Library for MSVCRT. DLL, retail version
Such a hint.
Support for threads is a later matter.
This also led to many CRT functions in the case of multithreading must have special support, can not simply use the CreateThread on OK.
Most CRT functions can be used in CreateThread threads, and the data says that only the signal () function is not allowed to cause the process to terminate. But you can use it not to say that there is no problem.
Functions such as malloc (), fopen (), _open (), Strtok (), CTime (), or localtime () require specialized thread-locally-stored blocks of data, which typically need to be created when a thread is built, If you use CreateThread, this block of data is not established, and then what happens. In such a thread, you can still use these functions and there is no error, in fact the function found that the pointer to this block is empty, it will build one, and then associate it with the thread, which means that if you use CreateThread to create a thread, and then use such a function, There will be a piece of memory unknowingly created, unfortunately, these functions do not delete it, and CreateThread and ExitThread can not know this matter, so there will be memory leak, in the process of frequently launched software (such as some server software), Sooner or later, your system will run out of memory resources.
_beginthreadex (internal also call CreateThread) and _endthreadex on this block of memory processing, so there is no problem. (no one will intentionally use CreateThread to create and then terminate with _endthreadex, and thread termination is best not to explicitly call the termination function, natural exit best.) )
When it comes to the problem of handle, _beginthread's corresponding function _endthread automatically calls CloseHandle, and _beginthreadex's corresponding function is not _endthreadex. So CloseHandle is going to call it anyway, but _endthread can help you do it without writing, and you need to write the other two. (Jeffrey Richter strongly recommended that as far as possible without explicit termination function, with natural exit, natural exit of course must write their own closehandle)

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.