c++ - 如何将参数传递给 boost::thread?

标签 c++ boost boost-thread

thread_ = boost::thread( boost::function< void (void)>( boost::bind( &clientTCP::run , this ) ) );  

是否可能 run 有这样的参数:

void clientTCP::run(boost:function<void(std::string)> func);

如果是的话应该如何编写我的 boost::thread 调用

谢谢。

最佳答案

以下代码 boost::bind( &clientTCP::run , this ) 定义了一个函数回调。它在当前实例 (this) 上调用函数 run。使用 boost::bind,您可以执行以下操作:

// Pass pMyParameter through to the run() function
boost::bind(&clientTCP::run, this, pMyParameter)

在此处查看文档和示例:
http://www.boost.org/doc/libs/1_46_1/doc/html/thread/thread_management.html

If you wish to construct an instance of boost::thread with a function or callable object that requires arguments to be supplied, this can be done by passing additional arguments to the boost::thread constructor:

void find_the_question(int the_answer);

boost::thread deep_thought_2(find_the_question,42);

希望对您有所帮助。

关于c++ - 如何将参数传递给 boost::thread?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5730747/

相关文章:

c++ - 二值图像,使用 OpenCV 的白点浓度

c++ - Windows 时间限制应用程序能否在时间到时将游戏退出全屏(DirectX?)模式?

c++ - 我是否需要 1 个读者和 1 个作者的互斥锁,我不介意丢失一些写入?

c++ - 如何使用 Boost.Variant 迭代一系列有界类型

c++ - 为什么将引用计数器值读取为对 volatile 常量的引用?

C++工作队列与阻塞

c++ - 多个互斥锁策略以及为什么库不使用地址比较

c++ - 如何线程化作为类方法的可调用函数

C++ 字符串函数只返回某些字符

c++ - 如何复制 strncat(原创)