error C2276; passing &member function to _beginthreadex
- From: "Robert Ellis" <robe_2k5@xxxxxxxxxxxxxxxxxxxx>
- Date: Wed, 1 Mar 2006 02:18:32 -0000
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
.
- Follow-Ups:
- Re: error C2276; passing &member function to _beginthreadex
- From: Scott McPhillips [MVP]
- Re: error C2276; passing &member function to _beginthreadex
- From: Igor Tandetnik
- Re: error C2276; passing &member function to _beginthreadex
- From: Victor Bazarov
- Re: error C2276; passing &member function to _beginthreadex
- Prev by Date: Re: I don't know why this code don't compile (22 lines)
- Next by Date: Re: How to convert TCHAR type to standard ISO C++ string?
- Previous by thread: Re: from iostream.h to iostream
- Next by thread: Re: error C2276; passing &member function to _beginthreadex
- Index(es):
Relevant Pages
|