Createthread (), _ beginthread (), and _ beginthreadex () contact and Difference

Source: Internet
Author: User

Createthread, _ beginthread, and _ beginthreadex are used to start threads.

Differences:

_ Beginthread is a subset of Functions of _ beginthreadex. _ beginthreadex is the Runtime Library Function of Microsoft C/C ++, and createthread is the function of the operating system. Although _ beginthread calls _ beginthreadex internally, It shields functions such as security features. Therefore, _ beginthread and createthread are not of the same level, and _ beginthreadex and createthread are completely functional alternatives, let's compare _ beginthreadex and createthread!

<Windows core programming> detailed introduction:

Note: to create a new thread, do not use createthread, instead use _ beginthreadex.
Why? Consider some variables and functions in the Standard C Runtime Library, such as errno, which is a global variable. Global variables are used
What will happen to multithreading? You must know. Therefore, a mechanism must exist so that each thread can reference its own
The errno variable does not touch the errno variable of another thread. _ beginthreadex assigns its own
Tiddata memory structure. This structure stores the values and addresses of many variables and functions such as errno ).
Associate tiddata with the thread through local thread storage. The specific implementation is available in threadex. C.
The end thread uses the _ endthreadex function to release the tiddata data block of the thread.


The function library of the CRT already exists before the emergence of threads, so the original CRT cannot really support threads, which leads to the selection of the CRT library during programming, when you check the CRT functions 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
This prompt!
The support for threads was a matter of time!
This also causes many CRT functions to have special support in the case of multithreading. It is not easy to use createthread.
Most of the CRT functions can be used in the createthread thread. According to the document, only the signal () function is not supported and the process is terminated! But it does not mean there is no problem!

Some CRT functions are like malloc (), fopen (), _ open (), strtok (), ctime (), or localtime () and other functions require a dedicated thread to locally store data blocks. This data block usually needs to be created when the thread is created. If createthread is used, this data block is not created, what will happen then? In such a thread, these functions can still be used without errors. In fact, when the function finds that the pointer to this data block is null, it creates one by itself and associates it with the thread, this means that if you use createthread to create a thread and then use such a function, there will be a piece of internal creation without knowing it. Unfortunately, these functions will not be deleted, createthread and exitthread cannot know this, so there will be a memory leak. In software with frequent thread startup (such as some server software), sooner or later the system's memory resources will be exhausted!

_ Beginthreadex (createthread is also called internally) and _ endthreadex handle this memory block, so there is no problem! (No one will intentionally use createthread to create and end with _ endthreadex, and it is best not to explicitly call the termination function for thread termination. It is best to exit naturally !)

Speaking of handle, the _ beginthread function _ endthread automatically calls closehandle, while the _ beginthreadex function _ endthreadex does not, therefore, closehandle is called no matter how it is, but _ endthread can help you to execute it without writing it. The other two types need to be written by yourself! (Jeffrey Richter strongly recommends that you do not need to terminate the function explicitly as much as possible. Instead, you must write closehandle yourself to exit automatically)

Example:

// Create/destroy
Bool cxthread: Create (void * pthreaddata)
{
// Get PTR to data
M_pthreaddata = pthreaddata;

// Create events
If (! Createthreadevents ())
Return false;

// Create thread
# If defined (usewin32thread)
M_hthread =
Createthread (null, 0, cxthread: threadproc, (lpvoid) This, 0, & m_idthread );
# Else
M_hthread = (handle)
_ Beginthreadex (null, 0, cxthread: threadproc, (lpvoid) This, 0,
(Unsigned int *) & m_idthread );
# Endif

If (m_hthread = 0)
Return false;

// Success so start thread
//: Resumethread (m_hthread );
Return true;
}


// Thread proc
# If defined (usewin32thread)
DWORD winapi cxthread: threadproc (lpvoid parameter)
# Else
Unsigned _ stdcall cxthread: threadproc (lpvoid parameter)
# Endif
{
If (! Parameter)
Return xthread_normal;

// Start thread
Cxthread * pthread = (cxthread *) parameter;
Int ret = pthread-> Run ();

// Exit the thread
# If defined (usewin32thread)
Exitthread (xthread_normal );
# Else
_ Endthreadex (xthread_normal );
# Endif

Return ret;
}

Trackback: http://tb.blog.csdn.net/TrackBack.aspx? Postid = 1826830

 

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.