C++ Threading: Referencing a member of a class instance

Associate
Joined
22 Jul 2004
Posts
230
Location
Nottingham, UK
I'm using a custom library to start a thread.
I want the threaded function to be a member of an instance of a class:
Code:
class A{
	public:
		void ThreadStart(void *);
};

A* a;

int main(){
	a = new A();
	
	CustomLib::StartAThread(a->ThreadStart,"",0); // Error Here
}

The error is:
Code:
error C3867: 'A::ThreadStart': function call missing argument list; use '&A::ThreadStart' to create a pointer to member.

I can work around it using this, but it isn't really the way i want todo it.
Code:
class A{
	public:
		void ThreadStart(void *);
};

A* a;

int main(){
	CustomLib::StartAThread(Start,"",0); // References a static function instead.
}

void Start(void *){
	a = new A();
	a->ThreadStart();
}
 
It looks like StartAThread is expecting a 'C' style function pointer, so you can't give it a class member method pointer.

Making ThreadStart a static method of class A would work.

Then do

Code:
CustomLib::StartAThread(a::ThreadStart,"",0);

Obviously making the method static has other implications, mainly every instance of the class uses the same instance of method, so it might not make sense for you to do that.
 
Back
Top Bottom