Create a thread to a class function
I'm trying to create a thread to a class function, but it doesnt work for me...
The function :
|
void Server::RecMsg(SOCKET &socket)
|
the call:
|
std::thread GetMessages(&Server::RecMsg, ClientSocket);
|
I get the error :
"Error 1 error C2064: term does not evaluate to a function taking 1 arguments"
Why so?
Thanks!
> term does not evaluate to a function taking 1 arguments"
> Why so?
To call a non-static member function, the object on which the member-function is to be called (one which provides the
this pointer) is required.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19
|
#include <iostream>
#include <thread>
struct A
{
int foo( int arg ) const { std::cout << "A::foo( " << arg << " )\n" ; return arg+2 ; }
};
int main()
{
A object ;
std::thread thread( &A::foo /* function */ , &object /* this */, 1234 /* arg */ ) ;
thread.join() ;
// object is moved to the thread
std::thread thread2( &A::foo, std::move(object), 5678 ) ;
thread2.join() ;
}
|
http://coliru.stacked-crooked.com/a/07bbe8bf8531208e
Topic archived. No new replies allowed.