I'm a beginning C++ programmer and I'm programming on a Linux machine.
I got this error:
cannot convert ‘void* (Network::*)(void*)’ to ‘void* (*)(void*)’ for argument ‘3’ to ‘int pthread_create(pthread_t*, const pthread_attr_t*, void* (*)(void*), void*)
It is comming from this line:
pthread_create(&thread_id,0,&Network::SocketHandler, (void*)csock );
The function I'm trying to call is:
void* Network::SocketHandler(void* lp)
I declared both functions in the header file as private.
Do any of you see what I'm doing wrong?
You are using a member function pointer where a regular function pointer is expected. A member function has an implicit extra parameter: this
. pthread_create
does not account for that.
You will have to make the function static
to be able to use it with pthread_create
. You can then use the void*
parameter to pass along what would otherwise be the this
pointer.
Personally, I would just ditch pthreads in favor of C++11 std::thread
, or boost::thread
if you don't have access to a C++11 implementation.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With