如何传递一个参数,以提高::线程? [英] How to pass an argument to boost::thread?
问题描述
thread_ =提高::线程(升压::功能<无效(无效)>(升压::绑定(安培; clientTCP ::来看,这一点)));
是有可能,运行有一个这样的说法:
无效clientTCP ::运行(升压:功能<无效(标准::字符串)> FUNC);
,如果是我的boost ::线程调用应该怎么写
感谢。
以下code 的boost ::绑定(安培; clientTCP ::来看,这一点)
定义一个回调函数。它调用函数运行
在当前实例(这个
)。随着的boost ::绑定你可以做到以下几点:
//传递pMyParameter通过对run()函数
提高::绑定(安培; clientTCP ::来看,这,pMyParameter)
查看文档和示例在这里:结果
<一href=\"http://www.boost.org/doc/libs/1_46_1/doc/html/thread/thread_management.html\">http://www.boost.org/doc/libs/1_46_1/doc/html/thread/thread_management.html
如果你想构建一个实例
升压::线程与函数或
可调用对象要求
要提供的参数,这可以是
通过传递额外的参数进行
升压::线程的构造函数:
块引用>无效find_the_question(INT the_answer);提高::线程deep_thought_2(find_the_question,42);
希望有所帮助。
thread_ = boost::thread( boost::function< void (void)>( boost::bind( &clientTCP::run , this ) ) );
is it possible that run has an argument like this :
void clientTCP::run(boost:function<void(std::string)> func);
and if yes how my boost::thread call should be written
Thanks.
解决方案The following code
boost::bind( &clientTCP::run , this )
defines a function callback. It calls the functionrun
on the current instance (this
). With boost::bind you can do the following:// Pass pMyParameter through to the run() function boost::bind(&clientTCP::run, this, pMyParameter)
See the documentation and example here:
http://www.boost.org/doc/libs/1_46_1/doc/html/thread/thread_management.htmlIf 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);
Hope that helps.
这篇关于如何传递一个参数,以提高::线程?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!