When I try to pass my function cleanup_thread()
to pthread_cleanup_push()
:
pthread_cleanup_push(cleanup_thread, arg);
I get the following compiler error:
error: no matching function for call to ‘__pthread_cleanup_class::__pthread_cleanup_class(<unresolved overloaded function type>, void*&)’
I guess its because void cleanup_thread(void *arg)
is a member function of my class and therefor has the this
pointer as first argument. So the signature of the function cleanup_thread() does not match. How can I tell pthread_cleanup_push()
to use my member function cleanup_thread()
?
The easiest solution is probably to make the function a static
member function, and pass the instance pointer as the argument.
Something like this:
struct data_struct
{
some_class* instance;
pthread_t thread_id;
};
class some_class
{
public:
...
static void cleanup_thread(void* arg)
{
data_struct* ptr = reinterpret_cast<data_struct*>(arg);
ptr->instance->private_cleanup_thread();
delete ptr;
}
private:
void private_cleanup_thread()
{
...
}
};
...
some_class* some_class_ptr = new some_class;
...
data_struct* data_ptr = new data_struct;
data_ptr->instance = some_class_ptr;
data_ptr->thread_id = some_thread_id;
pthread_cleanup_push(&some_class::cleanup_thread, data_ptr);
Collected from the Internet
Please contact [email protected] to delete if infringement.
Comments