EQuestionAnswers.com Computer/Electronics Questions and Answers
C, C++, VC++, COM/DCOM, DLL and more
#Login #Sign up  Facebook Twitter TGoogle+

#Previous question #Index of VC++/WIN32/MFC Questions #Next question


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.

/*Creates Worker thread*/
CWinThread* AFXAPI AfxBeginThread(AFX_THREADPROC pfnThreadProc, LPVOID pParam,
  int nPriority = THREAD_PRIORITY_NORMAL, UINT nStackSize = 0,
  DWORD dwCreateFlags = 0, LPSECURITY_ATTRIBUTES lpSecurityAttrs = NULL);
/*Creates UI thread*/
CWinThread* AFXAPI AfxBeginThread(CRuntimeClass* pThreadClass,
  int nPriority = THREAD_PRIORITY_NORMAL, UINT nStackSize = 0,
  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.
  • dwCreateFlags 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.
  • lpSecurityAttrs 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.

Return Value

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.

 Vote 0

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)  Like 0 Unlike 0

Dear Users,

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:

My Post:*
Secutiry Code: ******  *
Preview this compose before posting this in discussion forum.
  1. Use [Sxx] code to show smiles. Example [S02]
  2. Use [URL ], example [URL http://www.google.com/]
  3. To display code or un formatted text use [CODE] [/CODE]. Example: [CODE] printf("Hello world"); [/CODE]
 [S01]   [S02]   [S03]   [S04]   [S05]   [S06]   [S07]   [S08] 
 [S09]   [S10]   [S11]   [S12]   [S13]   [S14]   [S15]   [S16] 
 [S17]   [S18]   [S19]   [S20]   [S21]   [S22]   [S23]   [S24] 

Note: Only members are allowed, * fields are mandatory.