【问题标题】:boost::bind thread for pointer to function with argumentboost::bind 线程,用于指向带参数的函数的指针
【发布时间】:2013-10-06 16:04:18
【问题描述】:

我有一个函数 foo(myclass* ob) 我正在尝试使用 consumer_thread(boost::bind(&foo)(&ob)) 创建一个消费者线程

代码无法编译,我认为这是由于我将函数参数传递给函数指针的方式不恰当。

class myclass{
// stuff
}

void foo(myclass* ob){
// stuff
}

int main(){
myclass* ob = new myclass();
boost::thread consumer_thread()boost::bind(&foo)(&ob));
// stuff
}

我做错了什么?谁能在这里详细说明 boost::bind 以及如何使用函数参数传递函数指针?

提前致谢!

【问题讨论】:

    标签: c++ boost function-pointers boost-thread boost-bind


    【解决方案1】:

    您的代码示例有一些错误。这是一个固定版本,调用bind的返回值作为boost::thread构造函数中的唯一参数:

    boost::thread consumer_thread(boost::bind(foo, ob));
    

    但是您可以完全跳过对boost::bind 的调用,将函数及其参数传递给构造函数:

    boost::thread consumer_thread(foo, ob);
    

    【讨论】:

    • 谢谢.. 我对何时应该或不应该使用绑定有些不清楚,尽管我已经阅读了它。
    【解决方案2】:

    应该是bind(foo, ob)

    但是,我很确定boost::threadstd::thread 具有相同的接口,在这种情况下您根本不需要bind

    boost::thread consumer_thread(foo, ob);
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2010-12-27
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2020-04-14
      相关资源
      最近更新 更多