[英]No matching function call when passing member function of a class to pthread_cleanup_push()
When I try to pass my function cleanup_thread()
to pthread_cleanup_push()
: 当我尝试将函数
cleanup_thread()
传递给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. 我猜是因为
void cleanup_thread(void *arg)
是我的类的成员函数,因此具有this
指针作为第一个参数。 So the signature of the function cleanup_thread() does not match. 因此,函数cleanup_thread()的签名不匹配。 How can I tell
pthread_cleanup_push()
to use my member function cleanup_thread()
? 如何告诉
pthread_cleanup_push()
使用我的成员函数cleanup_thread()
?
The easiest solution is probably to make the function a static
member function, and pass the instance pointer as the argument. 最简单的解决方案可能是使函数成为
static
成员函数,并将实例指针作为参数传递。
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);
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.