Difference between beginthread beginthreadex createthread

Source: Internet
Author: User

Create a thread.
Unsigned long beginthread (void (cdecl * startaddress) (void *), unsigned stacksize, void * Arglist );
Unsigne dlong beginthreadex (void * Security, unsignedstacksize, unsigned (stdcall * startaddress) (void *), void * Arglist, unsignedinitflag, unsigned * thrdaddr );
Header file compatibility required by the routine
Beginthread <process. h> Win NT, Win 95
Beginthreadex <process. h> Win NT, Win 95
For additional compatibility information, see compatibility in Introduction
Library
Libc. Lib single-thread static library, retail version
Libcmt. Lib multi-threaded static library, retail version
Msvcrt. Lib msvcrt. dll input library, retail version
To use beginthread or beginthreadex, the application must be linked to one of the multi-threaded C Runtime libraries.
Return Value
If successful, these functions return the handle of the recently created thread. When an error occurs,BeginthreadReturn-1In this case, if there are too many threadsErrnoSetEagainIf the parameter is invalid or the stack size is incorrectErrnoSetEinval. When an error occurs,BeginthreadexReturn0In this caseErrnoAndDoserrnoAre set.
Parameters
Startaddress
The starting address of the routine that starts executing the new thread.
Stacksize
The stack size of the new thread is 0.
Arglist
The parameter table or null passed to the new thread.
Security
Security indicator of the new thread; it must be null for Windows 95 applications.
Initflag
The initial status of the new thread (0 is returned during running or createsuspend is returned when paused ).
Thrdaddr
The address of the new function.
Description
BeginthreadThe function creates a thread and starts executing the routine at startaddress. The routine at startaddress must useCdeclThe call Convention does not return values. When the thread returns from this routine, it terminates automatically.
. Beginthredex compiles Win32 create threadapi functions more closely than beginthread. beginthreadex differs from beginthread in the following aspects:
. Beginthreadex has three other parameters: initflag, security, and threadaddr. A new thread can be created in the paused state. It can be accessed using the specified security method (only in Windows NT) and thrdadar, which is the thread identifier.
The program at. startaddress is sentBeginthreadex, Must be usedStdcallCall conventions and a thread exit code must be returned.
If a failure occurs, beginthreadex returns 0 instead of-1.
The thread established by. eginthreadex is terminated by calling endthreadex.
You can explicitly call endthread or endthreadex to terminate a thread, but it automatically calls endthread or endthreadex when the thread returns from the routine passed as a parameter. Call endthread or endthreadex to terminate a thread to restore the resources allocated by the thread.
The endthread automatically closes the thread handle (unlike endthreadex). Therefore, when beginthread and endthread are used, the Win32 closehandle API function is called to not explicitly close the thread handle. This behavior is different from the Win32 exitthread API function.
Note: Do not call the win32exitthread API function for executable files linked to libcmt. Lib. This prevents the running system from claiming the allocated resources. Endthread and endthreadex require that the allocated thread resources be reclaimed and then the exitthread is called. When beginthread or beginthreadex is called, you do not need to transmit the thread stack address to these functions when the operating system processes stack allocation. In addition, the stacksize parameter can be 0. In this case, the operating system uses the same value as the stack specified in the main thread.
Arglist is a parameter transmitted to the recently established thread. It is usually the address of a data item, such as a string.
Arglist can be null if not required, but beginthread and beginthreadex must provide some values passed to the new thread. If any thread calls abort, exit, exit, or exitprocess, the thread used is terminated.

When writing C ++ code, I always remember one sentence: createthread should never be called. Instead, use the Visual C ++ Runtime library function _ beginthreadex.

It seems that the createthread function is a tiger. So why does Microsoft need to develop this function?

I will summarize the relevant information found on the Internet. I would like to thank you!

From windows core programming:

The createthread function is a Windows function used to create a thread. However, if you are writing C/C ++ code, you should never call createthread. Instead, use the Visual C ++ Runtime library function _ beginthreadex. If you do not use Microsoft's Visual C ++ compiler, your compiler vendor has its own createthred alternative function.

To enable the multi-threaded C and C ++ programs to run correctly, you must create a data structure and associate it with each thread that uses the C/C ++ Runtime library function. When you call the C/C ++ Runtime Library, these functions must be aware of the data blocks of the calling thread, so that they will not have adverse effects on other threads.

1. Each thread obtains its own tiddata memory structure allocated by the stack of the C/C ++ runtime database.

2. The address of the thread function passed to _ beginthreadex is stored in the tiddata memory block. Parameters passed to the function are also saved in the data block.

3. _ beginthreadex does call createthread internally, because this is the only way for the operating system to understand how to create a new thread.

4. When createtthread is called, it is notified to start the new thread by calling _ threadstartex instead of pfnstartaddr. Also, the parameter passed to the thread function is the tiddata structure rather than the pvparam address.

5. If everything goes well, the thread handle will be returned like createthread. If any operation fails, null is returned.

_ Beginthreadex and _ beginthread functions. The _ beginthread function has fewer parameters, so the fully-bit _ beginthreadex function is more limited.

For example, if _ beginthread is used, a new thread with security attributes cannot be created, a paused thread cannot be created, and the thread id value cannot be obtained.

The following is an excerpt from the csdn Holly () post for explanation, and I would like to thank you again.

Source: http://topic.csdn.net/t/20000926/10/31810.html

Holly ():

Oldworm provides good examples of use and also uses compilation control!

Let me explain the differences in theory:

Createthread, _ beginthread, and _ beginthreadex are used to start the thread. However, we can see that oldworm does not provide the _ beginthread method. The reason is simple. _ beginthread is a subset of _ beginthreadex, 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!

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)

Related Article

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.