C++: Creating new thread using pthread_create, to run a class member function

前端 未结 4 1121
面向向阳花
面向向阳花 2020-12-22 04:53

I have the following class:

class A
{
    private:
        int starter()
        {
             //TO_DO: pthread_create()
        }

        void* threadStar         


        
4条回答
  •  情深已故
    2020-12-22 05:24

    If you insist on using the native pthreads interface, then you must provide an ordinary function as the entry point. A typical example:

    class A
    {
    private:
        int starter()
        {
            pthread_t thr;
            int res = pthread_create(&thr, NULL, a_starter, this);
            // ...
        }
    public:
        void run();
    };
    
    extern "C" void * a_starter(void * p)
    {
        A * a = reinterpret_cast(p);
        a->run();
        return NULL;
    }
    

提交回复
热议问题