Saturday, August 17, 2013

Best design for windows multi thread in a c++ class

ISSUE: How to create windows multi-threaded program in  c++ ?


SOLUTION:  Now a day maximum project or software has multiple process running at a same process.Or a gui application needs to do some work on background which is blocking. Or may be you are going to design or create a network application ,so it becomes mandatory to be an expert of Threading now a days.


Here is  the template which I follow.

In header file add the followings --

#include <Windows.h>

static UINT __cdecl CallingFunctionForThread(LPVOID pParam);
UINT WorkingFunction();
 
 
In CPP file add the followings --

UINT __cdecl MyClass::CallingFunctionForThread(LPVOID pParam)
{
    MyClass *pMyClass= reinterpret_cast<MyClass*>(pParam);
    UINT ret = pMyClass->WorkingFunction();

    return ret;
}

UINT MyClass::WorkingFunction()
{ 
    // You can do anything as a class member function and run it on your thread.
}
Start thread  from any where by adding the followings --
 
 AfxBeginThread(
    CallingFunctionForThread,
    this); 
 

No comments:

Post a Comment

How to Generate and use the ssh key on Gerrit, github.io, gitlab, and bitbucket.

 Details can be found here -