Thursday, August 14, 2014

MFC Threads.


MFC distinguishes between two types of threads.
  • User interface(UI) Threads and 
  • Worked Threads.
The difference between the two is that UI threads have message loops and worker threads do not.UI threads can create windows and process messages sent to those windows.

Worker threads perform background tasks that receive no direct input from the user and therefore don't need windows and message loops.

Creating worker thread: 
The best way to launch a thread in an MFC Application is to call AfxBeginThread.
MFC defines two different versions of AfxBeginThread. One that starts a UI thread and another starts a worked thread.
AfxBeginThread creates a new CWinThread object launches a thread and ttachesa it to the CWinThread object and returns CWinThread pointer.

Syntax:
CWinthread * pThread = AfxBeginThread(Threadfunc,&threadInfo)

Above statement starts a worker thread and passes it to the application-defined data structure(&threadInfo) that contains input to the thread. Threadfunc  is the thread function the function that gets executed when the thread itself begin to execute.

Simple Thread function:
                                UINT Threadfunc(LPVOID pParam)
                                {
                                     UINT nIterations = (UINT)pParam;
                                     for(UINT i=0; i < nIterations ; i++)
                                        return 0;
                                 }

AfxBeginThread accepts as many as four additional parameters that specify the threads priority,stack-size,
Creation flags and security attributes.The complete function prototype is as follows.

   CWinThread * AfxBeginThread(AFX_THREADPROC pfnThreadProc,
                                                     LPVOID pParam,
                                                     int nPriority = THREAD_PRIORITY_NORMAL,
                                                     UINT nStackSize = 0,
                                                     DWORD dwCreateFlags = 0,
                                                     LPSECURITY_ATTRIBUTES lpSecurityAttrs = NULL)

  • nPriority specifies the threads execution priority.High-priority threads are scheduled for CPU time before low-priority threads. The default is THREAD_PRIORITY_NORMAL which assigns the same priority as the process that owns it.We can change thread's priority level at any time with CWinThread::SetThreadPriority.

  • The nstackSize parameter passed to AfxBeginThread specifies the threads maximum stack size. the 0 default nStackSize value allows the stack to grow as large as 1MB.
  • dwCreateFlags can be one of two values.The default value 0 tells the system to start executing the thread immediately.If CREATE_SUSPENDED is specified instead,the thread starts out in a suspended state and doesn't begin running until another thread calls CWinthread::ResumeThread on the suspended thread.

  • lpSecurityAttrs, is a pointer to a SECURITY_ATTRIBUTES structure that specifies the new threads security attributes and also tells the system whether child process should inherit the thread handle.The NULL default value assigns the new thread the same properties the thread that created it.

The Thread Function:

A thread function is a callback function,so it must be either a static class member function or a global function declared outside a class.It is prototyped this way " UINT ThreadFunc(LPVOID pParam)".

pParam is a 32-bit value whose value equals the pParam passed to AfxBeginThread.

String Table

A string table is a Windows resource that contains a list of IDs, values, and captions for all the strings of your application. For exa...