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

前端 未结 4 1092
面向向阳花
面向向阳花 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:01

    Do you have a reason for using pthreads? c++11 is here, why not just use that:

    #include 
    #include 
    
    void doWork()
    {
       while(true) 
       {
          // Do some work;
          sleep(1); // Rest
          std::cout << "hi from worker." << std::endl;
       }
    }
    
    int main(int, char**)
    {
    
      std::thread worker(&doWork);
      std::cout << "hello from main thread, the worker thread is busy." << std::endl;
      worker.join();
    
      return 0;
    }
    

提交回复
热议问题