Boost.Thread库中boost::thread() 无启动参数的解决方法

/**
 * Create only by new operator, delete by owner
 * joinable m_thread
 */
class LaThread {
   
private:
    boost::thread* m_thread;
 
    static static_run(LaThread* This) {
       This->run();
    }
 
protected:
    virtual void run() = 0;
 
public:
    LaThread() : m_thread(NULL) {}
    /**
     * Requires: threat ended
     */   
    virtual ~LaThread(){
       if( m_thread != NULL )
           delete m_thread;
    }
 
public:
    void start() {
       if( m_thread != NULL ) {
           throw std::exception(_T("m_thread != NULL"));
       }
       m_thread = new boost::thread( boost::bind(static_run, this) );
    }
 
    void join() {
       if( m_thread == NULL ) {
           throw std::exception(_T("m_thread == NULL"));
       }
       m_thread->join();
    }
};
 
class TestThread : public LaThread {
private:
    int arg;
public:
    TestThread( int arg ) {
       this->arg = arg;
    }
public:
    virtual void run() {
       _RPT3(_CRT_WARN, "[%d]\t[%s]\t[%s]\n", ::GetCurrentThreadId(), _T("Hi I'm new thread!"), __FUNCTION__);             
    }     
};
主要看点是:
new boost::thread( boost::bind(static_run, this) );
boost::bind使可以使 static_run(LaThread* This) 变成满足 boost::thread ( boost::function0 fuc )要求的函数。
C++ ISO 1998 的确并不是一个STL Container那么简单啊。还有boost::mem_fn呢。
解决这类别扭问题的方法的:
google: Boost Thread parameter
回复者(可能是Thread文档的开发者),说这样的说明将会加入到下个版本的文档中。


你可能感兴趣的:(thread,C++,c,C#,Google)