First introduce two kinds of thread:
worker thread : general thread, have no message pump, should do the real time costing works.
user-interface thread: a thread of message peeking is running. Only deal with messages.
UI thread
The first thing you must do when creating a user-interface thread is derive a class from CWinThread. You must declare and implement this class, using the DECLARE_DYNCREATE and IMPLEMENT_DYNCREATE macros. This class must override some functions and can override others. These functions and what they should do are presented in the following table.
Function |
Purpose |
ExitInstance |
Perform cleanup when thread terminates. Usually overridden. |
InitInstance |
Perform thread instance initialization. Must be overridden. And if return FALSE the thread will ends by itself. |
OnIdle |
Perform thread-specific idle-time processing. Not usually overridden. |
PreTranslateMessage |
Filter messages before they are dispatched to TranslateMessage and DispatchMessage. Not usually overridden. |
ProcessWndProcException |
Intercept unhandled exceptions thrown by the thread's message and command handlers. Not usually overridden. |
Run |
Controlling function for the thread. Contains the message pump. Rarely overridden. |
W orker thread
Only two steps are required to get your thread running: implementing the controlling function and starting the thread. It is not necessary to derive a class from CWinThread.
AfxBeginThread for worker thread
To begin execution of your worker thread, call AfxBeginThread, providing the following information:
l The address of the controlling function.
l The parameter to be passed to the controlling function.
l (Optional) The desired priority of the thread. The default is normal priority. For more information about the available priority levels, see SetThreadPriority in the Platform SDK.
l (Optional) The desired stack size for the thread. The default is the same size stack as the creating thread.
l (Optional) CREATE_SUSPENDED if you want the thread to be created in a suspended state. The default is 0, or start the thread normally.
l (Optional) The desired security attributes. The default is the same access as the parent thread. For more information about the format of this security information, see SECURITY_ATTRIBUTES in the Platform SDK.
Implementing the Controlling Function
When this function is entered, the thread starts, and when it exits, the thread terminates. This function should have the following prototype:
UINT MyControllingFunction( LPVOID pParam );
When the function terminates, it should return a UINT value indicating the reason for termination.
Controlling Function Example
UINT MyThreadProc( LPVOID pParam ) { CMyObject* pObject = (CMyObject*)pParam; if (pObject == NULL || !pObject->IsKindOf(RUNTIME_CLASS(CMyObject))) return 1; // if pObject is not valid // do something with 'pObject' return 0; // thread completed successfully } // inside a different function in the program . . . pNewObject = new CMyObject; AfxBeginThread(MyThreadProc, pNewObject); . . .
Start and stop
AfxBeginThread creates a new CWinThread object, calls its CreateThread function to start executing the thread, and returns a pointer to the thread. Instead of calling AfxBeginThread, you can construct a CWinThread-derived object and then call CreateThread.
To end the thread, call AfxEndThread from within the thread, or return from the controlling function of the worker thread.
Can not delete the CreateThread cause the destructor is protected member function and used by dynamic creation.
Normal Thread Termination
For a worker thread, use either the AfxEndThread function or a return statement. The return value signifies the reason for termination. Typically, 0 signifies successful completion
For a user-interface thread, from within the user-interface thread, call PostQuitMessage in the Platform SDK.
Premature Thread Termination
Call AfxEndThread from within the thread. Pass the desired exit code as the only parameter. This stops execution of the thread, deallocates the thread's stack, detaches all DLLs attached to the thread, and deletes the thread object from memory.
AfxEndThread must be called from within the thread to be terminated. If you want to terminate a thread from another thread, you must set up a communication method between the two threads.
Note:
MFC objects are not thread-safe at the object level, only at the class level. This means that you can have two separate threads manipulating two different CString objects, but not two threads manipulating the same CString object.
The CWinThread class is necessary to make your code and MFC fully thread-safe. Thread-local data used by the framework to maintain thread-specific information is managed by CWinThread objects. Because of this dependence on CWinThread to handle thread-local data, any thread that uses MFC must be created by MFC . For example, a thread created by the run-time function _beginthread, _beginthreadex cannot use any MFC APIs.