Member function as Thread

Hai,

I want to make a class member function to execute as a different Thread

class CMyThread
{
public:
static UINT Thread_1(LPVOID lpContext);

};

UINT CMyThread::Thread_1(LPVOID lpContext)
{

return 0;
}

and create thread like ...

AfxBeginThread(CMyThread::Thread_1,0,0);

Only if the member function is Static it will work.

If the function is not static it gives compile error.
CMyThread obj;
AfxBeginThread(obj.Thread_1,0,0);

error C2665: 'AfxBeginThread' : none of the 2 overloads can convert parameter 1 from type 'unsigned int (void *)'

How can a make a non static member function as a thread.

bai
[757 byte] By [baijut] at [2007-11-18 17:25:47]
# 1 Re: Member function as Thread
Originally posted by baijut
How can a make a non static member function as a thread.You can't - a callback function like a thread proc must be global or static (as member functions have an implicit this-pointer parameter and hence don't match the required signature for the callback). However, this has been asked soooo often here, and there's even a FAQ (http://www.dev-archive.com/forum/showthread.php?s=&threadid=231047) on this subject. It also explains how to pass the this-pointer to your static function yourself, so you can access class members from your thread proc.
gstercken at 2007-11-9 13:55:47 >
# 2 Re: Member function as Thread
Well you can do that in a more resonable and "ellegant" manner.
You may try to do that:
DWORD WINAPI PseudoThreadFunction( IN LPVOID vThreadParm )
{
CMyThread* pThreadParam = ( CMyThread* ) vThreadParm;
pThreadParam->Thread_1();
return 1;
}

class CMyThread
{

public:
int Thread_1( ) // This function will be executed by a thread The thread is run from the ExecuteThread_1
int ExecuteThread_1( );
};

int CMyThread::ExecuteThread_1( )
{

HANDLE hThread = NULL;
DWORD dwThreadID = 0;
int nTimeout = 5000;

try
{
hThread = CreateThread( NULL, // Pointer to thread security attributes
0, // Initial thread stack size, in bytes
PseudoThreadFunction,
this, // The argument for the new thread is a pointer to your MyThread
0, // Creation flags
&dwThreadID ); // Pointer to returned thread identifier
}
catch( CException* /* pException */ )
{
return false;
}

bool bFinished = false;
do
{

DWORD dwWaitResult = MsgWaitForMultipleObjects(
1,
& hThread,
FALSE,
nTimeout,
QS_ALLEVENTS | QS_SENDMESSAGE
);

switch ( dwWaitResult )
{
case WAIT_OBJECT_0 :
case WAIT_ABANDONED_0 :
{
bFinished = true;
break;
}
case WAIT_OBJECT_0 + 1 : // message(s) in queue
{
MSG msg;
while ( ::PeekMessage( &msg, NULL, 0, 0, PM_NOREMOVE ) == TRUE )
if ( !AfxGetApp( )->PumpMessage( ) )
{
::PostQuitMessage( 0 );
}
break;
}
default :
{
bFinished = true;
}
}
} while ( ! bFinished );

return 1;
}

You can now make a instance of your class and call the ExecuteThread_1(). i.e.:

CMyThread theInstance;
theInstance.ExecuteThread_1();

This will execute the Thread_1() function from your class in a separate thread.

If you need help screem.

Good Luck.
mr.speed at 2007-11-9 13:56:47 >
# 3 Re: Member function as Thread
The code as posted won't compile, since PseudoThreadFunction() is a global function and Thread_1( ) is private to CMyThread. PseudoThreadFunction() should be a static member of CMyThread, not global.
gstercken at 2007-11-9 13:57:58 >
# 4 Re: Member function as Thread
Originally posted by gstercken
The code as posted won't compile, since PseudoThreadFunction() is a global function and Thread_1( ) is private to CMyThread. PseudoThreadFunction() should be a static member of CMyThread, not global.

gstercken,

You are right. The Thread_1( ) MUST be public. That will do. Thanks. :)
mr.speed at 2007-11-9 13:58:53 >
# 5 Re: Member function as Thread
Originally posted by mr.speed
You are right. The Thread_1( ) MUST be public. That will do. As said above - the better solution would be to declare PseudoThreadFunction() as a static member of the class.
gstercken at 2007-11-9 13:59:52 >
# 6 Re: Member function as Thread
[Moved thread]
Andreas Masur at 2007-11-9 14:00:51 >