Function purpose: Create a thread.
Function Description:
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
Used library files
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 the call succeeds, these functions return the handle of the recently established thread. When an error occurs, _ beginthread returns-1. In this case, if there are too many threads, errno is set to eagain, if the parameter is invalid or the stack size is incorrect, errno is set to einval. When an error occurs, _ beginthreadex returns 0. In this case, both errno and doserrno are 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
The _ beginthread function establishes a thread and starts the execution of the routine at startaddress. The routine at startaddress must use the cdecl call Convention and there is no return value. When the thread returns from this routine, it terminates automatically.
. _ Beginthredex compiles Win32 create threadapi function 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 sent to _ beginthreadex, which must use the stdcall call Convention and must return a thread exit code.
If _ beginthreadex fails, 0 is returned instead of-1.
The. _ beginthreadex thread is terminated by calling _ endthreadex.
You can explicitly call _ endthread or _ endthreadex to terminate a thread. However, when the thread returns from a routine passed as a parameter, it automatically calls _ endthread or _ endthreadex. Call _ endthread or _ endthreadex to terminate a thread to restore the resources allocated by the thread.
_ Endthread automatically closes the thread handle (but _ endthreadex does not). Therefore, when _ beginthread and _ endthread are used, it is not explicitly closed by calling the Win32 closehandle API function. 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 need to reclaim the allocated thread resources and then call exitthread. When _ ininthread 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.
Why do we recommend that you use _ beginthread or _ beginthreadex instead of createthread?
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)