0

I have a connundrum in the use of the mfc function

AfxBeginThread(AFX_THREADPROC pfnThreadProc,
   LPVOID pParam,
   int nPriority = THREAD_PRIORITY_NORMAL,
   UINT nStackSize = 0,
   DWORD dwCreateFlags = 0,
   LPSECURITY_ATTRIBUTES lpSecurityAttrs = NULL 
);

Apparently the type AFX_THREADPROC is a reference. It is what worked. So in a class (constructor) I call this function to start a thread with thread routing call it MyThreadProc() is referenced as follows.

pfnThreadProc = &MyThreadProc;

I copied this usage from another program, which MyThreadProc() defined outside the class (call it USBCom). This worked fine. The thread got started. I debugged it to completion just fine. Great. Then I realized this thread routine had to call other member function to get it's worker thread duties done. I tried calling inside like

.. USBCom:MyMemberFunc() ..

to no avail. So then I moved this MyThreadProc() to within my class USBCom. Compiling I couldn't use this instanciation of AfxBeginThread().

I suppose the &MyThreadProc() was the problem (what it pointed to). Probably some standard about not allowing addresses of member functions being passed around -- that whole encapsulation thang.

So how do I fix this predicament. Where I can start a thread in a class where the thread proc is outside the class yet has access or perview to to member functions within the class that started it.

As some background this thread's purpose will be to read both the status of the USB Ports I am using as well as data being sent to me via USB 2.0. I have performance milestones to meet, so I wish this be lean. How do I do it??

Maddog

ps: I am communication with an FDDi FT240XS USB FIFO.

Michael Murdock
  • 129
  • 1
  • 9
  • 2
    `AFX_THREADPROC` is a function pointer, not a reference. It must be a free function or a static class member. `pParam` is used to pass arbitrary data to a thread. You can pass a `this`-pointer, for example. – IInspectable Mar 19 '16 at 21:59

2 Answers2

0

The confusion stems from the difference between using "&" as "address of" versus "reference"

What are the differences between a pointer variable and a reference variable in C++?

TL;DR:

int foo;
int *pointer = &foo;
int &reference = foo;

I hope this helps

Community
  • 1
  • 1
Jason De Arte
  • 467
  • 1
  • 3
  • 14
0

I think a found an answer, not to say that yours wouldn't have worked as well. It was compounded by the fact, that I had licensing issues that had to get resolved as well. That is done now.

Anyway my answer was that I had to make these methods public. I alread had a pointer to the object that had instantiated this class as

UINT MyThreadProc(LPVOID param)
{
  MyClass *pMyHndl = (MyClass*)param;
  pMyHndl->myPublicFunct();
}

where myPublicFunct() is a member function of class MyClass().

After I thought about, how silly was I..

Thanks for all the help.

Maddog

Michael Murdock
  • 129
  • 1
  • 9