Thread control

xiaoxiao2021-03-06  37

Threads' concept is in some previous operating systems, such as previous UNIX and Windows3.x, threads and processes in the same process in the same process space, while subces and parents The process runs in different spaces. This way, the different threads within the same process can be directly swapped directly through memory (not doing this for data synchronization reasons).

In addition, a process in Win32 has at least one thread, so the process is also called the main thread. When you create a process in the previous section, you also see that you can get a thread handle when you get the process handle. There are two types, windows threads and WORKER THREADs in Win32. The window thread will be able to create a window because the system will assign a message queue for the window after the window, and the message queue will be allocated for the working thread, so the working thread will consume less system resources.

Let's take a look at how to create threads.

In the MFC, the encapsulation class for thread function is provided, and CWINTHREAD we often use the CWINAPP class is derived from this class. Usually we use CWINTHREAD to create a window thread, the process is as follows:

Delicate new categories from CwinThread. The CWINTHREAD :: InitInstance () function is created in it and assumes the window pointer to m_pmainwnd. If you need to overload CwinThread :: exitInstance (), the function will be called when the window is destroyed. That is to say, the lifetime of the window thread is linked to the window of the window. Here is a simple example:

// Thread class definition

Class Cguithread: Public CWINTHREAD

{

PUBLIC:

Cguithread ();

Virtual Bool InitInstance (Void);

Virtual Int ExitInstance (Void);

}

CguithRead :: cguithread ()

{

/ / Set automatic deletion

m_bautodelete = true;

}

Bool Cguithread :: InitInstance (Void)

{

CWND * PWND = New

CWnd (); PWND-> Createex (0,

AFXREGISTERWNDCLASS (CS_HREDRAW | CS_VREDRAW),

"GUI Thread Window",

WS_OVERLAPPEDWINDOW | WS_VISIBLE,

CRECT (0, 0, 100, 100),

NULL,

0);

m_pmainwnd = pWnd;

Return (m_pmainwnd! = false);

}

Int cguithread :: EXIXSTANCE (VOID)

{

"GUI Thread EXIT / N");

Return CWINTHREAD :: EXITINSTANCE ();

}

// Create a GUI thread process

Void csam_sp_43dlg :: ONGUIT ()

{

CguithRead * p_thread1 = new

Cguithread; p_thread1-> createthread ();

// Use the default parameters, because cguithread is automatically deleted, so you don't need to save this pointer.

}

For work threads, you only need to provide a thread entry, that is, a function address is OK. When the working thread is started, the function will end when the function exits. In MFC we can CWinThread * AfxBeginThread (AFX_THREADPROC pfnThreadProc, LPVOID pParam, int nPriority = THREAD_PRIORITY_NORMAL, UINT nStackSize = 0, DWORD dwCreateFlags = 0, LPSECURITY_ATTRIBUTES lpSecurityAttrs = NULL); meaning of the parameters are as follows: pfunThreadProc: a function entry address, function The original shape should be like: uint mycontrollingfunction (lpvoid pParam); PPAram: is the parameter passed to the thread. NPRIORITY: Indicates the priority of the thread. Commonly used THREAD_PRIORITY_NORMAL, THREAD_PRIORITY_TIME_CRITILAL, THREAD_PRIORITY_ABOVE_NORMAL. NSTACKSIZE: To the stack size, if 0 means using the system default value. DWCREATEFLAGS: To create a thread, the thread is hanged after Create_Suspended to the thread is created. LPSecurityATTRS: is the security attribute. The return value of the function is a pointer to the CWINTHREAD class, which can be controlled to threads through it. When the thread function returns, the thread will be ended, and Void AFXITCODE) can be used inside the thread; the end thread is ended, and Nexitcode is exit code.

Here is a simple example:

// Work thread

Uint Workerthread (LPVoid PPARAM)

{

/ / Receive a window pointer, then set the window title

CWND * pstatimer = (cwnd *) PPARAM;

For (int i = 0; i <100; i )

{

// Trace ("Thread% D / N", I);

Char SZT [100];

Sprintf (SZT, "Worker Thread:% D", i);

PstatiMer-> SetwindowText (SZT);

Sleep (10);

}

Return 0; // Return and exit the thread

/ / Or call Void AFXITHREAD (UINT NEXITCODE);

}

// Create thread

Void csam_sp_43dlg :: OnWorkt ()

{

// m_statimer is a CSTATIC variable. Pass window pointer

AfxBeginthread (Workerthread, & m_statimer);

}

In the CWINTHREAD class, you will hang and restore the thread run through DWORD CWINTHREAD :: Suspend () and DWORD CWINTHREAD :: Suspend (). Get and set thread priority by int CWINTHREAD :: GetThreadPriority () and Bool Cwinthread :: SetThreadPriority (int nPriority).

In addition, the member variables in the CWINTHREAD class: m_hthread and m_nthreadid saves the thread handle and the thread ID number. Threads can also be operated by other API functions.

API function BOOL TERMINATTHREAD (Handle Hthread, DWORD DWEXITCODE); can enhance a thread outside the thread, but this is dangerous, because threads apply to certain resources may not be released, and it is also possible to cause crash of the process. So the recommended method is to set a marker when the thread is detected, rather than using a method of enforcing the thread from the outside. Download this section Demonstration code 20K

转载请注明原文地址:https://www.9cbs.com/read-72942.html

New Post(0)