提升线程示例显示错误它无法匹配函数调用'boost :: thread :: thread(<unresolved overloaded =“”function =“”type =“”>)'</unresolved>

时间:2011-07-11 10:22:36

标签: c++ boost-thread

我在创建namspace multithread_init时在main函数上初始化了线程,以便将set_multihthread类推送到命名空间。为什么在声明boost::thread之后它与调用boost::thread t(smulti.thread)的函数不匹配?

#define BOOST_THREAD_USE_LIB
#include <boost/thread.hpp>
#include <boost/thread/thread.hpp>
#include <iostream>
#include <vector>
#include <cstdlib>
#include <ctime>
#include <iostream>


#ifndef MULTITHREAD_INIT_HPP_
#define MULTITHREAD_INIT_HPP_
namespace multithread_init{
    class set_multithread{
    private:
        //t;
    public:
        void thread(){
            for(int i = 0; i < 5; i++){
                wait(1);
                std::cout<<" thread i value : "<<i<<std::endl;
            }
        }

        void wait(int seconds)
        {
          boost::this_thread::sleep(boost::posix_time::seconds(seconds));
        }

//      void multi_case(){
//          t.join();
//          boost::thread  t(thread);
//      }

    };
}

#endif /* MULTITHREAD_INIT_HPP_ */

主文件如下。

int main()
{

    /*thread */
    multithread_init::set_multithread smulti;
    boost::thread  t(smulti.thread);
    t.join();

}

1 个答案:

答案 0 :(得分:2)

您不能以这种方式传递成员函数。您需要将其绑定到对象

boost::thread  t(boost::bind(&multithread_init::set_multithread::thread, &smulti));