Threads are light weight process. Main application creates these light weight process to do certain tasks in a separate independent context. This way main process can offload main task into several threads. This is possible as Windows is a multithreaded operating system. Several miniature threads can run in parallel. They can exit when they are done or they can be put to suspended until next work load comes.
Worker vs UI
There are two types of threads worker thread and user interface thread.
A worker thread is a type of light weight process which is done in the background without any user interaction.
UI thread is a process where a top level window and user interaction messages are involved. We can say UI thread is a front end task whereas worker threads are back end tasks.
AfxBeginThread() is a MFC global function which creates a new thread. AfxBeginThread is an overloaded function.
It creates a worker thread if first argument is a thread function pointer else this can be a thread class for creating a UI thread.
AfxBeginThread creates a new CWinThread object, calls its CreateThread function to start executing the thread, and returns a pointer to the thread.
To end the thread, call AfxEndThread from within the thread, or return from the controlling function of the worker thread.
MFC CWinThread class is the wrapper for thread related works. Parent can control this thread via its member functions.
| 1 || /*Creates Worker thread*/
| 2 || CWinThread* AFXAPI AfxBeginThread(AFX_THREADPROC pfnThreadProc, LPVOID pParam,
| 3 || int nPriority = THREAD_PRIORITY_NORMAL, UINT nStackSize = 0,
| 4 || DWORD dwCreateFlags = 0, LPSECURITY_ATTRIBUTES lpSecurityAttrs = NULL);
| 5 ||
| 6 || /*Creates UI thread*/
| 7 || CWinThread* AFXAPI AfxBeginThread(CRuntimeClass* pThreadClass,
| 8 || int nPriority = THREAD_PRIORITY_NORMAL, UINT nStackSize = 0,
| 9 || DWORD dwCreateFlags = 0, LPSECURITY_ATTRIBUTES lpSecurityAttrs = NULL);|
- pfnThreadProc - Points to the thread procedure function for the worker thread. Cannot be NULL. This function must be declared as follows:
UINT ThreadProc( LPVOID pParam );
pThreadClass - The RUNTIME_CLASS of an object derived from CWinThread.
- pParam - Parameter to be passed to the controlling function as shown in the parameter to the function declaration in pfnThreadProc.
nPriority - The desired priority of the thread. If 0, the same priority as the creating thread will be used. For a full list and description of the available priorities, see SetThreadPriority in the Win32 Programmer’s Reference.
nStackSize - Specifies the size in bytes of the stack for the new thread. If 0, the stack size defaults to the same size stack as the creating thread.
Specifies an additional flag that controls the creation of the thread. This flag can contain one of two values:
- CREATE_SUSPENDED - Start the thread with a suspend count of one. The thread will not execute until ResumeThread is called.
- 0 - Start the thread immediately after creation.
Points to a SECURITY_ATTRIBUTES structure that specifies the security attributes for the thread. If NULL, the same security attributes as the creating thread will be used. For more information on this structure, see the Win32 Programmer’s Reference.
Pointer to the newly created thread object.
You have viewed 1 page out of 109. Your VC++ learning is 0.00% complete. Login to check your learning progress.
Similar topics related to this section
Programming Windows, Fifth Edition (Developer Reference)
Programming Applications for Microsoft Windows (Microsoft Programming Series)
Programming Windows with MFC, Second Edition
Visual C++ Programming
Programming Microsoft Visual C++, Fifth Edition
| #1 webmaster Sun 27 Apr/2014 03:01:04(GMT) || 0 || 0|
We are pleased to inform you that a forum/blog has been incorporated with www.mybestnotes.co.in. You are welcomed to add your comments, requests, codes, solutions and feedback to it. Please login(if already a member) or signup(for free) to avail to this facility.
Your message goes here: