pthreads and C++

后端 未结 2 991
既然无缘
既然无缘 2021-01-23 15:12

I am playing with C++ and pthreads and so far so good. I can access a class member function if it\'s static and I\'ve read that I can access a normal class member functions if I

2条回答
  •  被撕碎了的回忆
    2021-01-23 15:57

    you can try boost thread library and use boost::bind() Here's an example,

    class MyThread
    {
    public:
        MyThread( /* Your arguments here */) : m_thread(NULL)
        {
            m_thread = new boost::thread(boost::bind(&MyThread::thread_routine, this));
        }
    
        ~MyThread()
        {
            stop();
        }
    
        void stop()
        {
            if (m_thread)
            {
                m_thread->interrupt(); 
                m_thread->join();
            }
        }
    
    private:
        void thread_routine() {... /* you can access a/b here */}
    
    
    private:
        int a;
        int b;
        boost::thread *m_thread;
    };
    

提交回复
热议问题