Why use _ beginthreadex () instead of createthread?

Source: Internet
Author: User
Tags strtok

When you plan to implement a multi-thread (non-MFC)ProgramWill you select a single-threaded CRT (C Runtime Library? If your answer is no, there will be another question. Have you selected createthread to create a thread? Most people may immediately answer yes. Unfortunately, this is a wrong choice.
Let me explain my conclusions and I will tell you why later.

For multi-threaded (non-MFC) programs
-Use malloc (), free (), new
-Call stdio. h or IO. H, including fopen (), open (), getchar (), write (), printf (), errno
-Use floating-point variables and floating-point operation functions
-Call functions that use static buffers, such as asctime (), strtok (), and rand.
You should use a multi-threaded CRT with _ beginthreadex (this function only exists in multi-threaded CRT). In other cases, you can use a single-threaded CRT with createthread.

Because for the generated thread, _ beginthreadex performs additional bookkeeping work for the above operations than createthread. For example, it helps strtok () to prepare a buffer for each thread.
However, in rare cases, multi-threaded programs do not use the above functions (such as memory allocation or IO), so instead of thinking about whether to use _ beginthreadex or createthread every time, it is better to finalize _ beginthreadex with a stick.

Of course, you may use Win32 to handle memory allocation and IO. At this time, you can indeed use a single-thread CRT with createthread, because the IO task has been transferred from the CRT to Win32. In this case, you should usually use heapalloc and heapfree to handle memory allocation, and use createfile or getstdhandle to process Io.

another important thing is that although _ beginthreadex returns an unsigned long, it is actually a thread handle (in fact, _ beginthreadex calls createthread internally ), so you should end it with closehandle. Do not use exitthread () to exit the thread created by _ beginthreadex, which will lead to the loss of the chance to release the bookkeeping data. Use _ endthreadex.

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.