The example of this article describes the implementation method of the C + + encapsulated thread class. Share to everyone for your reference. The specific methods are as follows:
Copy Code code as follows:
Notification message to the main window
#define Wm_cutterstart Wm_user +//wParam = XXX lparam==xxxx
/*
When calling this class outside, you only need isrunning () startxxx (XXX) suspendxxx () resumexxx () stopxxx ()
*/
/*
M_bcontinue is detected in the real work code dosomething, set to false at Exit and class destructor, and set to True at reset and at construction to continue working inside
M_brunning is detected in startxxx suspendxxx resumexxx, set to false at construction time and reset, set to True when _threadentry gets WaitForSingleObject
External through isrunning to get whether is running
*/
Class CMyClass
{
Public
Work exit code
Enum exitcode{
Exitsuccess,//Successful completion of the task
Exituserforce,//user terminated
Exiterror,//source file Error
};
Constructors
CMyClass (HWND hwndnotify); Receive Window Handle
Properties open to the outside
BOOL isrunning () const {return m_brunning;} Foreign
Operation Open to the outside
BOOL startxxx (XXX);
BOOL suspendxxx ();
BOOL resumexxx ();
void Stopxxx ();
Concrete implementation
Public
~cfilecutter (); destructor
Protected
Resetting parameter information and status flags
void Reset ();
The real work core code
void DoSomething ();
Worker threads
UINT friend _threadentry (LPVOID lpparam);
Status flag
BOOL m_bcontinue; Whether to continue to work dosomething detection, if in dosomething not m_bcontinue, stop work
BOOL m_brunning; is in a working state
Synchronize the above two sets of data
Critical_section M_cs; Data Gard
Private
Globally valid data for the life cycle of an object
HWND m_hwndnotify; Window handle that accepts message notification events
HANDLE m_hworkevent; Notification of event object handles to begin work
cwinthread* M_pthread; Worker threads
BOOL M_bsuspend; Pause Flag
BOOL M_bexitthread; Exit flag
};
Structure
Cmyclass::cmyclass ()
{
Initializing Global valid variables
M_hwndnotify = hwndnotify;
M_bexitthread = FALSE;
M_bsuspend = FALSE;
To create a wait event object
M_hworkevent =:: CreateEvent (NULL, FALSE, FALSE, NULL);
Create a worker thread
M_pthread = AfxBeginThread (_cutterentry, this, thread_priority_normal, 0, create_suspended, NULL);
M_pthread->m_bautodelete = FALSE;
M_pthread->resumethread ();
Initializing active variables during work
M_bcontinue = TRUE; The work function is not interrupted, the flag is true, and the value is detected in the work function
m_brunning = FALSE; The thread function is WaitForSingleObject, so it is false
Creating critical Code Snippets
:: InitializeCriticalSection (&m_cs);
}
Internal worker Threads
UINT _threadentry (LPVOID lpparam)
{
Get a pointer to a CMyClass object
cmyclass* Pmyclass = (cmyclass*) Lpparam;
Iterate over a user's work request
while (:: WaitForSingleObject (pmyclass->m_hworkevent, INFINITE) = = Wait_object_0 &&
!pmyclass->m_bexitthread)
{
Set the status flag to indicate that you are working
:: EnterCriticalSection (&pcutter->m_cs);
pmyclass->m_brunning = TRUE;
:: LeaveCriticalSection (&pcutter->m_cs);
Start a real job
Pmyclass->dosomething ()
Be ready to take on new assignments
Pmyclass->reset (); In this function, set the value of each flag
}
return 0;
}
void Cmyclass::reset ()
{
:: EnterCriticalSection (&m_cs);
Reset Status Flag
M_bcontinue = TRUE;
m_brunning = FALSE;
:: LeaveCriticalSection (&m_cs);
}
Cmyclass::~cmyclass ()
{
Set End Flag
M_bexitthread = TRUE;
Set Force exit Flag
:: EnterCriticalSection (&m_cs);
M_bcontinue = FALSE;
:: LeaveCriticalSection (&m_cs);
It's important, ******************************************.
Prevent threads from waiting on m_hworkevent events
:: SetEvent (M_hworkevent);
Make sure that the worker thread ends
:: WaitForSingleObject (M_pthread->m_hthread, INFINITE);
Free All Resources
:: CloseHandle (M_hworkevent);
::D eletecriticalsection (&m_cs);
Delete M_pthread;
}
BOOL cmyclass::startxxx (XXX)
{
if (m_brunning)
return FALSE;
Notifies the thread to start working
:: SetEvent (M_hworkevent);
return TRUE;
}
BOOL cmyclass::suspendxxx ()
{
if (!m_brunning)
return FALSE;
Pausing worker threads
if (!m_bsuspend)
{
M_pthread->suspendthread ();
M_bsuspend = TRUE;
}
return TRUE;
}
BOOL cmyclass::resumexxx ()
{
if (!m_brunning)
return FALSE;
Wake worker Thread
if (m_bsuspend)
{
M_pthread->resumethread ();
M_bsuspend = FALSE;
}
return TRUE;
}
void Cmyclass::stopxxx ()
{
Set Force exit Flag
:: EnterCriticalSection (&m_cs);
M_bcontinue = FALSE;
:: LeaveCriticalSection (&m_cs);
Prevent a thread from being paused
Resumecutter ();
}
-------------------------Implementation code-------------------------//
The real work code
void CMyClass::D osomething ()
{
Notify the user of an error
::P ostmessage (m_hwndnotify, wm_xxx, Exiterror, 0);
Notify the user to start work
::P ostmessage (m_hwndnotify, Wm_xxxstart, 0, XX);
First, determine whether to terminate execution
if (!m_bcontinue)
{
Releasing resources
xxxx
if (!m_bexitthread)
::P ostmessage (m_hwndnotify, Wm_xxxxstop, XX, XX);
Return
}
Notify the user that work is done
::P ostmessage (m_hwndnotify, Wm_cutterstop, exitsuccess, ncompleted);
}
I hope this article will help you with the C + + program design.