c++ - boost::bind thread 用于指向带参数的函数的指针

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

我有一个函数 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 以及如何传递带有函数参数的函数指针吗?

提前致谢!

最佳答案

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

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

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

boost::thread consumer_thread(foo, ob);

关于c++ - boost::bind thread 用于指向带参数的函数的指针,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19100206/

相关文章:

c++ - 在类声明中处理指向成员函数的指针

c++从decltype返回类型中删除noexcept

c++ - spirit X3 : Custom number parser yield unexpected leading zero in the result

c++ - 只读 std::map?

c++ - 如何使函数指针常量

c - 了解C语言中函数指针的typedef

c++ - 如何在 RPi3 上的交叉编译应用程序的主窗口上显示边框和标题栏?

c++ - Vim + OmniCppComplete : Completing on Class Members which are STL containers

c++ - 在编译时检测是否存在默认构造函数

c++ - 当同名文件夹已存在时,如何使用 boost 创建新文件夹?