C++ 如何将参数传递给boost::thread? thread\=boost::thread(boost::function(boost::bind(&clientTCP::run,this));
run是否可能有如下参数:C++ 如何将参数传递给boost::thread? thread\=boost::thread(boost::function(boost::bind(&clientTCP::run,this));,c++,boost,boost-thread,C++,Boost,Boost Thread,run是否可能有如下参数: thread_ = boost::thread( boost::function< void (void)>( boost::bind( &clientTCP::run , this ) ) ); void clientTCP::run(boost:function func); 如果是,我的boost::thread调用应该如何编写 谢谢。下面的代码boost::bind(&clientTCP::run,this)定义了一个函数回调。它在
thread_ = boost::thread( boost::function< void (void)>( boost::bind( &clientTCP::run , this ) ) );
void clientTCP::run(boost:function func);
如果是,我的boost::thread调用应该如何编写
谢谢。下面的代码
boost::bind(&clientTCP::run,this)
定义了一个函数回调。它在当前实例上调用函数run
(this
)。使用boost::bind,您可以执行以下操作:
void clientTCP::run(boost:function<void(std::string)> func);
请参阅此处的文档和示例:如果您希望构造一个实例 使用函数或 需要的可调用对象 要提供的参数,可以是 通过传递附加参数来完成 到boost::thread构造函数:
希望这能有所帮助。我只是想指出,对于未来的工作,Boost默认情况下是按值传递参数的。因此,如果要传递引用,可以使用
boost::ref()
和boost::cref()
方法,后者用于常量引用
我认为您仍然可以使用&
操作符进行引用,但我不确定,我一直使用boost::ref
void find_the_question(int the_answer);
boost::thread deep_thought_2(find_the_question,42);
要添加参数,只需添加一个参数:
thread_ = boost::thread( &clientTCP::run , this );
这咬了我一口。谢谢你。
thread_ = boost::thread( boost::function< void (void)>( boost::bind( &clientTCP::run , this ) ) );
thread_ = boost::thread( &clientTCP::run , this );
thread_ = boost::thread( &clientTCP::run , this, f );