Thread-related stuff

Source: Internet
Author: User

1.C language function, call _beginthread ();

2.API function, call CreateThread ();(This function is _beginthread and AfxBeginThread at all )

3.MFC function, call AfxBeginThread ();

AfxBeginThread is the global function of MFC, which is the encapsulation of CreateThread.

CreateThread is the Win32 API function, AfxBeginThread is eventually tuned to CreateThread.

Xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx
1>.
Specifically, the CreateThread function is the API function that Windows provides to the user, which is the standard form of the SDK, used in the

Process to take into account the synchronization and mutual exclusion of processes, inter-process synchronization mutually exclusive, such as a series of factors that lead to the deadlock of the operating system, use up

More cumbersome, beginners in the use of the time may produce unpredictable errors, it is recommended to use AfxBeginThread, is compiled

The original CreateThread function is encapsulated with MFC programming (of course, as long as the project properties are modified, the console and the Win32 project

can be called) and _beginthread is the run-Library function of C.

2>
When using AfxBeginThread,
The thread function is defined as: the UINT _yourthreadfun (LPVOID pparam) parameter must be so

When using CreateThread,
The function of the thread is defined as: DWORD WINAPI _yourthreadfun (LPVOID pparameter)

The two are essentially the same,
However, AfxBeginThread returns the CWinThread pointer, which means that it will be a new CWinThread object, and this object will be deleted automatically at the end of the thread run.

Creatthread, it returns a handle, and if you don't use CloseHandle, you can safely understand the thread state,

The last time Closehandle,windows will release resources.

_beginthreadex is Microsoft's C/s + + Runtime library function, CreateThread is the function of the operating system, _beginthreadex by calling CreateThread to achieve, but more than createthread do a lot of work.

Xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx

Using the AfxBeginThread function in MFC makes it easy to create threads and to wait, wake, and so on to threads.
1. Function prototype
cwinthread* afxbeginthread//return value: A thread object that points to a new thread.

(

Afx_threadproc Pfnthreadproc,//The entry function of the thread, the declaration must be as follows: UINT mythreadfunction (LPVOID pparam);

LPVOID Pparam,//pass in the parameters of the thread, note that it is of type: LPVOID, so we can pass a struct into the thread.

int npriority = thread_priority_normal,//thread priority, typically set to 0. Let it have a common priority with the primary thread.

UINT nstacksize = 0,//Specifies the size of the stack for the newly created thread. If 0, the newly created thread has a stack of the same size as the main thread.

DWORD Dwcreateflags = 0,//Specifies how the line threads after the thread is created. You can specify a value of two:

create_suspended: After a thread is created, it is suspended until ResumeThread is called;

0: Start running when the thread is created.

Lpsecurity_attributes Lpsecurityattrs = null//points to a security_attributes structure, which is used to flag the security of the newly created thread

If NULL, then the newly created thread will have the same security as the main path.

);

Thread creation
The general creation process is as follows:
Define a work function, in general your thread is performing the task according to the function's function:
UINT mythreadfunction (LPVOID pparam)
{
function body
return 0;
}
You can then create the thread as follows:
cwinthread* Mythread=afxbeginthread (mythreadfunction, Pparam, thread_priority_normal, 0, 0, NULL);
4, the thread waits and wakes up
(1) Let the thread wait (temporarily pending):
Mythread->suspendthread ();
(2) to wake the suspended thread:
Mythread->resumethread ();
5. View Thread Status:
DWORD Code;
GetExitCodeThread (Mythread->m_hthread, &code);
if (code==still_active) {//thread is still executing}
else {//thread stops executing}
6. End Thread
TerminateThread (mythread->m_hthread, 0);

Xxxxxxxxxxxxxxxxxxxxxxxxxxxxx

cwinthread* AfxBeginThread

(

cruntimeclass* pthreadclass,//A function pointer to a worker thread, cannot be empty. And the function of the worker thread must be so declared:

int npriority = Thread_priority_normal,

UINT nstacksize = 0,

DWORD dwcreateflags = 0,

Lpsecurity_attributes lpsecurityattrs = NULL

);

Xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx

Xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx

CreateThread

function function: Create thread

Function Prototypes:

Handlewinapicreatethread (

lpsecurity_attributeslpthreadattributes,//represents the security attribute of a thread kernel object, which is typically passed in null to use the default settings

size_tdwstacksize,//indicates the size of the line stacks space. Passing in 0 means using the default size (1MB).

lpthread_start_routinelpstartaddress,//represents the address of a thread function that a new thread executes, and multiple threads can use the same function address.

lpvoidlpparameter,//the arguments passed to the thread function.

The dworddwcreationflags,//specifies an additional flag to control the creation of the thread, and 0 indicates that the thread can be dispatched immediately after it is created.

If create_suspended, it means that the thread was created and paused so that it could not be dispatched until ResumeThread () was called.

LPDWORDLPTHREADID//returns the ID number of the thread, and passing in null means that it does not need to return the thread ID number.

);

function return value: The handle of the new thread was successfully returned, and Null was returned by failure.

//The simplest to create multithreaded instances#include <stdio.h>#include<windows.h>//Child thread FunctionsDWORD WINAPI threadfun (lpvoid pM) {printf ("the thread ID number of the child thread is:%d\n child thread output Hello world\n", GetCurrentThreadID ()); return 0;}//The main function , the so-called main function is actually the main thread execution function. intMain () {printf ("the simplest to create a multithreaded instance \ n"); printf ("--by Morewindows (http://blog.csdn.net/MoreWindows)--\n\n"); HANDLE HANDLE= CreateThread (NULL,0, Threadfun, NULL,0, NULL);    WaitForSingleObject (handle, INFINITE); return 0;}

Xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx

AfxBeginThread and CreateThread Specific differences

Specifically, CreateThread This function is provided to the user by Windows API function, is the standard form of the SDK, in the process of use to take into account the process of synchronization and mutual exclusion of the relationship between the process of mutual exclusion and so on a series of causes of the operating system deadlock factors, used more cumbersome, Beginners in the use of the time may produce unpredictable errors, it is recommended to use AfxBeginThread, is the compiler to the original CreateThread function encapsulation, with MFC programming (of course, as long as the project properties modified, Both the console and the Win32 project can be called) and _beginthread is the run-Library function of C.

When using AfxBeginThread, the thread function is defined as:

The essence of the two is the same, but AfxBeginThread returns a CWinThread pointer, that is, he will be a new CWinThread object, and this object is automatically deleted (at the end of the thread run), the inconvenience to us is not to get its state, Because it's possible at any time this pointer is pointing to an already invalid memory area, so when used (if you need to know its health) first create_suspended let him hang, then M_bautodelete=false, then ResumeThread, Finally, don't delete that pointer.

Creatthread is more convenient, it returns a handle, if you do not use CloseHandle, you can safely understand the thread state, the last time Closehandle,windows will release resources, So I generally use creatthread, convenient.

If using MFC programming, do not use CreateThread, if just use the runtime Library, with _begingthread, in short, do not use CreateThread easily. This is because functions in MFC and RTL may use some of the common variables they encapsulate, meaning that both AfxBeginThread and _beginthread have their own startup code that CreateThread does not have.

It is possible to have problems using MFC's classes and RTL functions in threads created with CreateThread. If you are using the assembly to write the Win32 program and do not call the MFC and RTL functions in the thread function, then use CreateThread is fine, or you are using the C write thread function, but you are careful not to call the RTL function is not a problem.

CreateThread is an interface provided by the operating system, while AfxBeginThread and _beginthread are the compiler's encapsulation of it.

If possible, do not call _beginthread, but should call _beginthreadex. and the corresponding _endthreadex. This is a C + + run-time function. However, with _beginthread, you cannot create a new thread with a security attribute, you cannot create a paused thread, or you cannot get a thread id,_endthread, it does not take parameters,

This means that the exit code for this thread must be hard-coded to 0. The two functions are called in _beginthreadex and _endthreadex. CreateThread do not make a direct call.

(go) thread-related stuff

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.