passing function pointer
-
Right now, We have a ClxThreadClass that provides the capablity to create single worker thread for the class that inherits it. The function below shows the class member function that creates and starts the new thread. The desire worker code reside in _ThreadFunc.
The Current working design /*************************************************/ bool ClxThreadClass::CreateNewThread() { if( m_hThread == NULL ) { // _beginthreadex(Security attributes, stack, Thread proc, Thread param, creation mode, Thread ID) m_hThread = (HANDLE)_beginthreadex(NULL, 0,_ThreadFunc, this, CREATE_SUSPENDED, &uiThreadId); // if ( NULL != m_hThread) { //SetThreadPriority(hThread, THREAD_PRIORITY_HIGHEST); ResumeThread( m_hThread ); m_bActive = true; return true; }else{ m_bActive = false; } } return true; }
However, We sometimes would like to use different function other than _ThreadFunc. How can we overload CreateNewThread member function to take an external and pass to beginthreadex. This external obect is not a member function of base or inherited class.bool ClxThreadClass::CreateNewThread( void *ptr ) { if( m_hThread == NULL ) { // _beginthreadex(Security attributes, stack, Thread proc, Thread param, creation mode, read ID) m_hThread = (HANDLE)_beginthreadex(NULL, 0, ptr, this, CREATE_SUSPENDED, &uiThreadId); // if ( NULL != m_hThread) { //SetThreadPriority(hThread, THREAD_PRIORITY_HIGHEST); ResumeThread( m_hThread ); m_bActive = true; return true; }else{ m_bActive = false; } } return true; }
Scott Dolan Jernie Corporation Engineering & Manufacturing Software, Hardware, & Enclosures
-
Right now, We have a ClxThreadClass that provides the capablity to create single worker thread for the class that inherits it. The function below shows the class member function that creates and starts the new thread. The desire worker code reside in _ThreadFunc.
The Current working design /*************************************************/ bool ClxThreadClass::CreateNewThread() { if( m_hThread == NULL ) { // _beginthreadex(Security attributes, stack, Thread proc, Thread param, creation mode, Thread ID) m_hThread = (HANDLE)_beginthreadex(NULL, 0,_ThreadFunc, this, CREATE_SUSPENDED, &uiThreadId); // if ( NULL != m_hThread) { //SetThreadPriority(hThread, THREAD_PRIORITY_HIGHEST); ResumeThread( m_hThread ); m_bActive = true; return true; }else{ m_bActive = false; } } return true; }
However, We sometimes would like to use different function other than _ThreadFunc. How can we overload CreateNewThread member function to take an external and pass to beginthreadex. This external obect is not a member function of base or inherited class.bool ClxThreadClass::CreateNewThread( void *ptr ) { if( m_hThread == NULL ) { // _beginthreadex(Security attributes, stack, Thread proc, Thread param, creation mode, read ID) m_hThread = (HANDLE)_beginthreadex(NULL, 0, ptr, this, CREATE_SUSPENDED, &uiThreadId); // if ( NULL != m_hThread) { //SetThreadPriority(hThread, THREAD_PRIORITY_HIGHEST); ResumeThread( m_hThread ); m_bActive = true; return true; }else{ m_bActive = false; } } return true; }
Scott Dolan Jernie Corporation Engineering & Manufacturing Software, Hardware, & Enclosures
Maybe something like this:
typedef unsigned (__stdcall *PTHREADFUNC)(void *);
...
bool ClxThreadClass::CreateNewThread(PTHREADFUNC pThreadFunc)
{
if( m_hThread == NULL )
{
// _beginthreadex(Security attributes, stack, Thread proc, Thread param, creation mode, read ID)
m_hThread = (HANDLE)_beginthreadex(NULL, 0, pThreadFunc, this, CREATE_SUSPENDED, &uiThreadId);...
}
"Great job, team. Head back to base for debriefing and cocktails." (Spottswoode "Team America")
-
Maybe something like this:
typedef unsigned (__stdcall *PTHREADFUNC)(void *);
...
bool ClxThreadClass::CreateNewThread(PTHREADFUNC pThreadFunc)
{
if( m_hThread == NULL )
{
// _beginthreadex(Security attributes, stack, Thread proc, Thread param, creation mode, read ID)
m_hThread = (HANDLE)_beginthreadex(NULL, 0, pThreadFunc, this, CREATE_SUSPENDED, &uiThreadId);...
}
"Great job, team. Head back to base for debriefing and cocktails." (Spottswoode "Team America")
Mark Salsbery wrote:
typedef unsigned (__stdcall *PTHREADFUNC)(void *);
if this function points to global function, it will work .. if it point to class member function.. then there would be some trouble.. i.e. we have to change the Funtion ptr declaration and pass the pointer of the class to that function!
"Opinions are neither right nor wrong. I cannot change your opinion. I can, however, change what influences your opinion." - David Crow
cheers, Alok Gupta Global Interface Table: An Easy Way to Marshal an Interface Pointer[new] VC Forum Q&A :- I/ IV Support CRY- Child Relief and You
-
Mark Salsbery wrote:
typedef unsigned (__stdcall *PTHREADFUNC)(void *);
if this function points to global function, it will work .. if it point to class member function.. then there would be some trouble.. i.e. we have to change the Funtion ptr declaration and pass the pointer of the class to that function!
"Opinions are neither right nor wrong. I cannot change your opinion. I can, however, change what influences your opinion." - David Crow
cheers, Alok Gupta Global Interface Table: An Easy Way to Marshal an Interface Pointer[new] VC Forum Q&A :- I/ IV Support CRY- Child Relief and You
Right. The OP stated "This external obect is not a member function of base or inherited class" which soundedread like it wasn't a class method to me. Mark
"Great job, team. Head back to base for debriefing and cocktails." (Spottswoode "Team America")
-
Right. The OP stated "This external obect is not a member function of base or inherited class" which soundedread like it wasn't a class method to me. Mark
"Great job, team. Head back to base for debriefing and cocktails." (Spottswoode "Team America")
Mark Salsbery wrote:
he OP stated "This external obect is not a member function of base or inherited class" which soundedread like it wasn't a class method to me.
oops my mistake.. it seems work is taking over me [:)]
"Opinions are neither right nor wrong. I cannot change your opinion. I can, however, change what influences your opinion." - David Crow
cheers, Alok Gupta Global Interface Table: An Easy Way to Marshal an Interface Pointer[new] VC Forum Q&A :- I/ IV Support CRY- Child Relief and You