Attaching Member function of a class in pthread

前端 未结 1 912
离开以前
离开以前 2020-12-18 14:07
pthread_t thread1;
pthread_create(&thread1,NULL,.......,NULL);
// Here I want to attach a thread to a member function of class

How can I pass t

1条回答
  •  时光说笑
    2020-12-18 14:17

    You need to create a free extern "C" function as a trampoline:

    class foo
    {
    public:
        void *thread_func();
    };
    
    extern "C" void *thread_func(void *arg)
    {
        return static_cast(arg)->thread_func();
    }
    
    foo f;
    pthread_create(..., thread_func, &f);
    

    0 讨论(0)
提交回复
热议问题