• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 443
  • Last Modified:

Thread controlling function as non-global

I am using Visual c++ 5 to join two previous c++ programs and am using multithreading to let them run simultaneously.

I have built the GUI in a project and would like to add the other program to run as a worker thread when required.

I wan't the worker thread to have access to members of MFC classes in the original GUI thread. The problem I am having is in calling the worker thread from within a member function of the GUI thread.

I am trying to do this with code like this:

AfxBeginThread( (AFX_THREADPROC ) CMatchThread::MatchThreadProc, m_pPose);

where the actual controlling function is defined in the CMatchThread class as follows:

static UINT MatchThreadProc(LPVOID);

The error I get is this:
error C2440: 'type cast' : cannot convert from 'overloaded function type' to 'unsigned int (__cdecl *)(void *)'
                                                     None of the functions with this name in scope match the target type

I can't understand what's wrong with my typecast, I know it's possible to declare the controlling function as a non-global, where am I going wrong?
0
gavinpat
Asked:
gavinpat
1 Solution
 
kahhoeCommented:
hi gavinpat,

If I remember correctly, thread only work with C functions.  There are issue like stack and heap size to set too.  

One solution is to create a C function thread to run your class functions.  For example,

threadfunc1()
{
  CMatchThread::MatchThreadProc();  
}

Try to look for ACE (Adaptive Communication Envirnoment, I think) for information on how to implement multi-thread class.  Or just buy thread.h++ from roguewave.



Best Regards,
Kah Hoe
0
 
BridgeCommented:
Pass the class object as the parameter to the thread function.
0
 
gavinpatAuthor Commented:
Bridge, surely passing the class object as the parameter would mean that the thread becomes a GUI thread instead of a worker thread? I don't really want the overhead of a message pump slowing down the rest of the program.
0

Featured Post

Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

Tackle projects and never again get stuck behind a technical roadblock.
Join Now