Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/cplusplus/127.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C++ 在pthread中附加类的成员函数_C++_C - Fatal编程技术网

C++ 在pthread中附加类的成员函数

C++ 在pthread中附加类的成员函数,c++,c,C++,C,如何在上述代码中传递类的成员函数。您需要创建一个免费的extern“C”函数作为蹦床: pthread_t thread1; pthread_create(&thread1,NULL,.......,NULL); // Here I want to attach a thread to a member function of class class-foo { 公众: void*thread_func(); }; 外部“C”void*线程函数(void*arg) { 返回static

如何在上述代码中传递类的成员函数。

您需要创建一个免费的
extern“C”
函数作为蹦床:

pthread_t thread1;
pthread_create(&thread1,NULL,.......,NULL);
// Here I want to attach a thread to a member function of class
class-foo
{
公众:
void*thread_func();
};
外部“C”void*线程函数(void*arg)
{
返回static_cast(arg)->thread_func();
}
福福;
pthread_create(…,thread_func,&f);

不要忘记捕获所有异常。如果由于异常而退出线程,则程序可能会终止。
class foo
{
public:
    void *thread_func();
};

extern "C" void *thread_func(void *arg)
{
    return static_cast<foo *>(arg)->thread_func();
}

foo f;
pthread_create(..., thread_func, &f);