static thread function access non-static class member in C++

前端 未结 2 867
时光说笑
时光说笑 2021-01-03 08:22
Class Test{
    int value;
    static void* thread_func(void* args){
        value++;
    }
    void newthread(){
        pthread_create(&thread_func,...);
    }         


        
2条回答
  •  Happy的楠姐
    2021-01-03 08:46

    Let thread_func take a pointer to an object of the class as argument.

    static void* thread_func(void* pThis)
    {
        static_cast(pThis)->value++;
    }
    

    In case, this method wants to take some other information as well, put both in another struct/class and pass that in.

提交回复
热议问题