error C2276; passing &member function to _beginthreadex

Tech Tip: Click here to run a free scan for Windows Errors and optimize PC performance



Hi,

I think this topic has been covered in groups before from varying different
angles.

Trying to launch a new thread from within a class by passing the address of
a member function to _beginthreadex. It seems to have been well-documented
in groups that, for some purposes, it may be appropriate to declare the
member function as "static" but I've not been able to find a compile-able
syntax by going down that road. Also documented is the idea that this
problem stems from the fact that each member function has a hidden "context"
i.e. a reference to the class object itself, and that therefore when passing
the function we must fully-scope it in the form "&class::function"; however
I've not been able to find a compile-able syntax on that basis either, and
most of those discussions seem to focus on ATL or MFC or other stuff that
I'm not using.

So... a sample of my code - and the first, most important question probably
is, am I making a fundamental mistake in trying to pass a member function to
_beginthreadex, all inside a class?; or I am okay with my approach
"in-principle" and simply need to find the right syntax?

<.h>

#include <stdio.h>
#include <process.h>
#include <string>
#include <vector>
#include <WINSOCK2.H> // Winsock v2
#include <WS2TCPIP.H>
#include <WSPIAPI.H>


class SXClass
{
private:
unsigned __stdcall RecvReplyLine(void* pArguments);
int get_reply(int &ReplyCode);
CRITICAL_SECTION protect_data;
HANDLE reply_line_read;
int last_read; /* success/failure of last read */
SXClass();
virtual ~SXClass();
};

</.h>

<.cpp>

SXClass::SXClass() // construct
{
} ;

SXClass::~SXClass() // de'struct
{
} ;

int SXClass::get_reply(int &ReplyCode) /* this is the function that creates
the new thread */
{ // blah blah...
//
HANDLE hThread; //new thread handle
unsigned threadID; //new thread id

// Create the new thread....
hThread = (HANDLE)_beginthreadex(NULL, 0, &RecvReplyLine, NULL, 0,
&threadID);
//*** THE LINE above throws C2276 ***
//hThread = (HANDLE)_beginthreadex(NULL, 0, &SXClass::RecvReplyLine,
NULL, 0, &threadID); // no good
//hThread = (HANDLE)_beginthreadex(NULL, 0, &(SXClass::RecvReplyLine),
NULL, 0, &threadID); // no good

/* Second thread is now executing. Wait for completion event */
WaitForSingleObject(reply_line_read,MS_REPLY_TIMEOUT); /*or timeout*/

//blah blah.... clean up thread CloseHandle etc etc
return(0);
};

unsigned __stdcall SXClass::RecvReplyLine(void* pArguments) /* this is the
function that I want to run ON the new thread */
{ /* n.b. I'm not sure if I need the "(void* pArguments)" above, I
ripped the definition-"style" of this function from a MSDN example */

// blah blah.. do some work...............
SetEvent(reply_line_read);
return(0); // implies exit_thred()
};

</.cpp>

Apologies if I've missed out any code here - suffice to say it all compiles
fine; all the relevant headers are in place etc. Just the line marked above
that causing the compile problem. People in groups facing similar problems
seems to have been advised to use a calling syntax along the lines of
"&this->RecvReplyLine" but I do not like the idea of marching into global
spaces, which is my understanding of "this" (You will have to forgive my
ignorance if I'm making no sense - VB programmer delving into VC++ here).

Any thoughts appreciated. Please be gentle - still very much learning.
(maybe bit off a bit more than I can chew this time..lol)

Cheers,
Robert








.



Relevant Pages

  • Re: where is the template instance?
    ... >>What compiles and runs? ... >>definition, an in class member function definition and a declaration of, ... header file with a class definition, an in class member function definition ... I can provide it; however people complain when you ...
    (alt.comp.lang.learn.c-cpp)
  • Re: error C2276; passing &member function to _beginthreadex
    ... to declare the member function as "static" but I've not been able to find ... a compile-able syntax by going down that road. ... absolute incompatibility between nonstatic functions and _beginthreadex. ... static thread function use it to call a nonstatic member function. ...
    (microsoft.public.vc.language)
  • Re: A pointer in a class points to an other member function in the same class?
    ... Also wrong syntax. ... The first thing you need to do is retrieve the function pointer. ... The second thing to do is to actually call the member function using the ... the object that pObj points to is needed twice: ...
    (comp.lang.cpp)
  • Re: static member function wrapper
    ... Address of a member function, ... > trying to work through the syntax but as with most things mine here is ... it doesn't matter that bogus is private. ...
    (alt.comp.lang.learn.c-cpp)
  • Re: Question about Thread
    ... >> Remove this cast, fix the problem instead and try again. ... You appear to be calling ... a member function, when you need to be passing the address of a non-member ...
    (microsoft.public.vc.language)