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.