Creating a new thread in a multithreaded programming Windows environment

Source: Internet
Author: User

In the Win32 API, the basic function for creating a thread is CreateThread, and _beginthread (ex) is a function of the C + + runtime. Why should there be two of them? Because there are some functions in the C + + runtime that use global quantities, unsafe problems can occur if you use the functions of these C + + runtime libraries with CreateThread. Instead, _beginthreadex handles these global variables so that each thread has a separate "global" amount.

So, if your programming only calls Win32 API/SDK, you can rest assured that with CreateThread, if you want to use the C + + runtime, you will use _beginthreadex, and you need to choose using Multithread Lib/dll in the compilation environment.

The C + + runtime Library has two functions to create threads, and the other is _beginthread, and the difference between them is to see MSDN yourself:

Often their explanation is that it is easy to cause a memory leak. This explanation is not wrong in itself, but it is not fully explained and detailed. So that many novice blindly trust the sentence, where all are used _beginthreadex function, or pretend not to see the use of CreateThread function. There was a time when I was very confused about this problem, I do not know exactly with that is right. Because I have more than once seen direct calls to the CreateThread function in many authoritative code. Is the authority wrong?? With a skeptical attitude to find a large number of information and books, and finally understand the key to the problem, here to do a description of that sentence is a perfect.

I won't explain the difference between _beginthreadex and CreateThread, it's easy to find. We just need to know a problem: _beginthreadex is a function of a C runtime library, CreateThread is a system API function, _beginthreadex internally calls CreateThread. So all the books emphasize that the problem with memory leaks is because the _beginthreadex function allocates a heap structure when creating a thread and is associated with the thread itself, and we call this structure the TIDDATA structure, which is associated with the thread itself via the thread local memory TLS. Our incoming thread entry function is stored in this structure. The role of Tiddata in addition to saving the entry address of the thread function, there is another important function is: C Run-time library some functions need to use this structure to save and get some data, such as the errno thread global variables. This is the most important thing.

When a thread calls a run-time library function that requires a TIDDATA structure, the following happens:

The Run-time library function tries to tlsgetvalue the address of the thread data block, and if not, the function assigns a TIDDATA structure to the field and is associated with the line threads, so the problem arises, if the thread is not terminated by the _endthreadex function. This structure will not be revoked and a memory leak will occur. In general, however, it is not recommended to use the _endthreadex function to end a thread because it contains a exitthread call.

Find out the specific reason for the memory leak, we can say: As long as the created thread does not use some runtime library functions that require the TIDDATA structure, our memory is safe. Therefore, the preceding sentence should be said to be perfect:

"Never call the system's own CreateThread function to create a new thread, but use _beginthreadex unless you never use a run-time library function that requires a TIDDATA structure in your thread"

This need to tiddata structure of the function is a bit troublesome, in Houtie's "Win32 multithreaded Programming" in the book said:

If you do something in any thread except the main thread, you should use the multithreaded version of the C Runtime library and use _beginthreadex and _endthreadex:

    1. Use malloc () and free (), or new and delete;
    2. Use any function declared in stdio.h or io.h;
    3. Use floating-point variables or floating-point arithmetic functions;
    4. Call any of the runtime functions that use a static buffer, such as asctime (), strtok (), or rand ().

Addition info refer to:

Http://support.microsoft.com/kb/104641/en-us

http://msdn.microsoft.com/en-us/library/ms682453 (vs.85). aspx

Some other related articles:

_beginthreadex and CreateThread

http://blog.chinaunix.net/space.php?uid=17102734&do=blog&id=2830124

CreateThread (), _beginthread () and _beginthreadex () Contact and difference

Http://www.cppblog.com/jans2002/archive/2011/11/21/69135.html

Creating a new thread in a multithreaded programming Windows environment

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.