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

2023-07-19C/C++开发问题
3

本文介绍了如何将参数传递给 boost::thread?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

问题描述

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

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

is it possible that run has an argument like this :

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

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

and if yes how my boost::thread call should be written

谢谢.

推荐答案

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

The following code boost::bind( &clientTCP::run , this ) defines a function callback. It calls the function run 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)

请参阅此处的文档和示例:
http://www.boost.org/doc/libs/1_46_1/doc/html/thread/thread_management.html

See the documentation and example here:
http://www.boost.org/doc/libs/1_46_1/doc/html/thread/thread_management.html

如果你想构造一个实例带有函数的 boost::thread 或需要的可调用对象要提供的参数,这可以是通过传递额外的参数来完成到 boost::thread 构造函数:

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);

希望有所帮助.

这篇关于如何将参数传递给 boost::thread?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

The End

相关推荐

无法访问 C++ std::set 中对象的非常量成员函数
Unable to access non-const member functions of objects in C++ std::set(无法访问 C++ std::set 中对象的非常量成员函数)...
2024-08-14 C/C++开发问题
17

从 lambda 构造 std::function 参数
Constructing std::function argument from lambda(从 lambda 构造 std::function 参数)...
2024-08-14 C/C++开发问题
25

STL BigInt 类实现
STL BigInt class implementation(STL BigInt 类实现)...
2024-08-14 C/C++开发问题
3

使用 std::atomic 和 std::condition_variable 同步不可靠
Sync is unreliable using std::atomic and std::condition_variable(使用 std::atomic 和 std::condition_variable 同步不可靠)...
2024-08-14 C/C++开发问题
17

在 STL 中将列表元素移动到末尾
Move list element to the end in STL(在 STL 中将列表元素移动到末尾)...
2024-08-14 C/C++开发问题
9

为什么禁止对存储在 STL 容器中的类重载 operator&amp;()?
Why is overloading operatoramp;() prohibited for classes stored in STL containers?(为什么禁止对存储在 STL 容器中的类重载 operatoramp;()?)...
2024-08-14 C/C++开发问题
6