Multi-threaded start
Multi-thread is not a new noun, there are many articles that introduce multithreaded articles in MSDN, but MSDN is all English, which is not conducive to reading. In order to let more friends master multi-threaded technology, I wrote this article using multi-threaded technology in VC , if there is any deficiencies, please do not advice.
First, the creation of threads
The MFC supports two types of threads, Worker threads, and UI threads, which are used in Worker threads. To create a Worker thread, the easiest way is to use the AFXBEGINTHREAD () function. The prototype of this function is as follows:
CWINTHREAD * AFXBEGINTHREAD (
AFX_THREADPROC PFNTHREADPROC,
LPVOID PPARAM,
INT nPriority = Thread_Priority_NORMAL,
Uint nstacksize = 0,
DWORD dwcreateflags = 0,
LPSecurity_attributes LPSecurityATTRS = NULL
);
Parameter Description:
PFNTHREADPROC
Thread control functions, you can implement threads in this function.
PParam
Used to pass parameters to thread, if you don't need to pass any parameters to thread, you can set it to NULL.
NPRIORITY
The priority of the new thread, the default value indicates that the priority of the new thread is the same as current priority.
NSTACKSIZE
The stack size of the new thread (in bytes), 0 means using the default stack size.
DWCreateFlags
This value must be 0 or create_suspended, using 0 to indicate the control function of the thread immediately.
LPSecurityATTRS
Specify security properties to the new thread, usually can be NULL.
return value:
If it fails, return null, otherwise point to the new CwinThread object.
The CWINTHREAD object generated by this method is automatically deleted at the end of the thread. There is a member variable m_bautodelete in CWINTHREAD, which controls whether the CWINTHREAD object is automatically deleted. To set this variable, you must generate threads in a hang. Here is a simple example:
#include
UINT MythreadProc (LPVOID); // Worker Thread Control Function
int main ()
{
CWINTHREAD * pthread = NULL;
Pthread = AFXBEGINTHREAD (MythreadProc, Null,
Thread_Priority_NORMAL,
0, CREATE_SUSPENDED;
ASSERT (PTHREAD);
Pthread-> m_bautodelete = false;
Pthread-> ResumeThread ();
WaitforsingleObject (pthread-> m_hthread, infinite);
// Waiting for the thread to end, this function will be introduced later.
Delete pthread;
Return 0;
}
Uint MythreadProc (LPVOID PDATA)
{
/ / Perform time-consuming work in this function
......
}
Second, communication between threads
In multi-threaded programs, inevitably communicate with other threads, to implement communication between threads, the easiest way is to use the postthreadMessage () function, the prototype is as follows:
Bool PostthreadMessage
DWord idthread, // message thread identifier for threads to be delivered
UINT MSG, // The message to be sent wPARAM WPARAM,
LParam LPARAM
);
return value:
If the message is successfully sent, the return value is not 0, otherwise 0.
The following example sends a MY_WM_QUIT custom message to the main thread:
......
DWORD DWTHREADID;
CMYAPP * PAPP = (CMYAPP *) AFXGetApp ();
DWTHREADID = PAPP-> M_NTHREADID; // Main thread ID
PostthreadMessage
DWTHREADID,
MY_WM_QUIT,
(Wparam) 0,
(LParam) 0);
......
Third, waiting for the thread
The easiest way to wait for the thread is to use the waitforsingleObject () function, the prototype of this function is as follows:
DWORD WAITFORSINGLEOBJECT
Handle Hhandle, // Waiting for the object of Handle
DWORD dwmilliseconds // waiting time
);
return value:
If the function fails, returns wait_failed; if the target is turned into an excitation state, return to WAIT_Object_0; wait until the waiting time is over, will return to wait_timeout; if the thread has a MUTEX (more information about Mutex, See MSDN and returns Wait_abandoned before the end of the thread is completed.
The dwmilliseconds parameter can be set to 0, and the function returns immediately. If the Handle waiting for the target has been excited, return to WAIT_Object_0, otherwise returns Wait_Timeout, so you can save a lot of time.
Fourth, conclude
Multi-threading technology is not difficult to master, but to write multithreaded programs that can run correctly, they will come down. I use multithreaded technology to write a software splitable file. This software uses the WORKER thread to split, merge files, and main threads simultaneously displays a cancel dialog box, which can click the cancel button in the cancel dialog to cancel the file split. ,merge. For details, please visit my website http://www.0735.com.cn/Whstudio.