Using boost::lambda with boost::thread

Using boost::lambda with boost::thread

本文关键字:boost thread with lambda Using      更新时间:2023-10-16

我在 Boost 上的线程中使用 lambda 时遇到了问题。

代码应该将函数(字符串)的结果放在向量的指定索引中。

std::vector< string > results(size);
std::vector< boost::thread > threads;
for(  int i = 0; i < size; i++ ) {
    threads.push_back( boost::thread( results.at( i ) = getAString( x,y,zed ) ) );
}

我将如何在这里使用 Boost::lambda?

请不要提及使用 c++11 语法。 我被迫使用的系统无法支持支持 c++11 的编译器。 谢谢!

在您的情况下,我想知道 lambda 函数的好处是什么。C++中的 Lambda 最适合已填充容器上的 STL 算法。在您的情况下,您希望使用 lambda 填充向量results,这不是库想要的那种东西。你可能会有一场艰苦的战斗。http://www.boost.org/doc/libs/1_49_0/doc/html/lambda.html#introduction

话虽如此,可以创建直接输入 rvalue 的 lambda:http://www.boost.org/doc/libs/1_49_0/doc/html/lambda/le_in_details.html#lambda.rvalues_as_actual_arguments虽然在您的情况下,我不确定可以采用组成字符串时使用的参数的固定函数有什么好处。

此外,我相信线程函数

不能返回值,因此您的线程函数将不得不将results.at(i)的位置作为参数。