Click to See Complete Forum and Search --> : Member function as Thread
baijut
March 2nd, 2004, 07:55 AM
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
gstercken
March 2nd, 2004, 08:01 AM
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.codeguru.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.
mr.speed
March 2nd, 2004, 08:54 AM
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.
gstercken
March 2nd, 2004, 09:01 AM
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.
mr.speed
March 2nd, 2004, 09:06 AM
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. :)
gstercken
March 2nd, 2004, 09:13 AM
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.
Andreas Masur
March 2nd, 2004, 10:44 AM
[Moved thread]
codeguru.com
Copyright 2007 Jupitermedia Corporation All Rights Reserved.