A multi threaded animation application in MFC by radsarma

   Thread is the smallest executable unit of code within an application. It is the path of execution of a process. An executing Instance of an application is a process while any specific job involving an application is the task and a process can contain many threads.

   The Windows environment has the ability to simultaneously process multiple processes and threads to enable multitasking. An application can be either Single or Multithreaded. A multi-threaded application is executed by windows by allocating resources in a very organized way.

   A Single Threaded will usually have only one thread i.e. The application?s Main thread and a Multi Threaded will have one Main and several additional threads. E.g. Auto save , Background Printing. In WIN 32 environment, small chunks of CPU time are allocated for every competing thread. The small size of time slice is approx. 20 milliseconds which makes it appear that it is processed simultaneously.

  There are two kinds of threads which can be created in MFC namely Worker Threads & User Interface Threads.

Worker Threads:

  These are Useful chunks of program code for a specific purpose. They communicate with global variables and synchronization events.

User Interface Threads:

   The main thread of the application, typically for user interface. The CWinApp itself does the work of the main thread, but the application thread should be derived from the CWinThread class. They can have a message loop.

   Threads can be suspended, resumed, put to sleep or even terminated.

Thread termination:

   Worker thread execute “return” or calls AfxEndThread(). Another thread can call “::TerminateThread” but this may lead to resource allocation leaks Better to signal thread or set global variable that you want it to terminate itself. For User Interface threads, we can send a WM_QUIT message.

Thread Implementation:

   Threads can be created within an application through the AfxBeginThread().This function creates worker threads for many small chunks of code to be executed simultaneously.

Creating a Thread :

a) Associate a message handler for a menu , toolbar , or any control update message.

b) In the command handler use the AfxBeginThread()

c) Pass the address of the thread function i.e. The function where the unit of code to be executed is written.

d) Pass the HWND of the parent application window through the GetSafeHwnd().

e) Pass the priority of the thread as THREAD_PRIORITY_NORMAL etc.

f) Control shifts on meeting this statement to the portion of code within the function.

Example :

CWinthread *pthread1;
pthread1=AfxBeginThread(Thread1,GetSafeHwnd(),THREAD_PRIORITY_NORMAL);
UINT Thread1(LPVOID lp)
{
Messagebox(?Thread In Execution?);
}

Thread Status Tracking:

   The status of a thread in execution can be tracked through the pointer which it returns to the CWinThread class.

Example :

CWinThread *thr;
DWORD status;
Thr=AfxBeginThread(Thread1,GetSafeHwnd(),THREAD_PRIORITY_NORMAL);
::GetExitCodeThread(thr->m_hThread,&status);
If ( status == STILL_ACTIVE )
{
Messagebox ( ?Thread In Execution? );
}

Controlling Threads

The priority of a thread can be changed to boost or reduce performance to prefer other important job through SetThreadPriority()

Example :

BOOL flag;
flag=thr-> SetThreadPriority (THREAD_PRIORITY_ABOVE_NORMAL);

   An example with the Source Code attached. This contains an MFC Application which can animate the images in a multi threaded fashion. Extract the Contents of the Multithreaded_Animation.zip in any folder & open Animate.dsw file in Visual C++. Compile & Execute….

Attachments:

Project Files : Multithreaded_Animation.zip