![](/img/trans.png)
[英]Is there a way to pass a member function to pthread_cleanup_push?
[英]No matching function call when passing member function of a class to pthread_cleanup_push()
當我嘗試將函數cleanup_thread()
傳遞給pthread_cleanup_push()
:
pthread_cleanup_push(cleanup_thread, arg);
我收到以下編譯器錯誤:
error: no matching function for call to ‘__pthread_cleanup_class::__pthread_cleanup_class(<unresolved overloaded function type>, void*&)’
我猜是因為void cleanup_thread(void *arg)
是我的類的成員函數,因此具有this
指針作為第一個參數。 因此,函數cleanup_thread()的簽名不匹配。 如何告訴pthread_cleanup_push()
使用我的成員函數cleanup_thread()
?
最簡單的解決方案可能是使函數成為static
成員函數,並將實例指針作為參數傳遞。
像這樣:
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.