c++ - Boost 线程示例显示错误,它无法匹配调用 'boost::thread::thread(<unresolved overloaded function type>)' 的函数

标签 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();

}

最佳答案

您不能以这种方式传递成员函数。你需要将它绑定(bind)到对象上

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

关于c++ - Boost 线程示例显示错误,它无法匹配调用 'boost::thread::thread(<unresolved overloaded function type>)' 的函数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6648725/

相关文章:

c++ - boost asio 类似信号量的解决方案

c++ - 从网络服务器传输套接字文件

c++ - 为什么我重载的 operator+ 在向自身添加变量时会在返回时使 'this' 未初始化?

c++ - 实现第三方方法虚拟化

mfc - BOOST 1.35 到 1.43 的升级导致 __pRawDllMain 的链接器错误(mfc 相关)

c++ - boost::thread 数据结构的大小在荒谬的一面?

c++ - 从指针到 uint32 的类型转换

c++ - 编译时检查变量是否有符号/无符号

c++ - 在执行过程中休眠一个线程

c++ - 如何在指向另一个类的c++中使用线程