2012-06-06 130 views
0

我在使用Boost的线程中使用lambda问题。boost :: lambda with boost :: thread

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

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的编译器。谢谢!

回答

0

在你的情况,我想知道lambda函数的好处是什么。 C++中的Lambdas最适合已经填充的容器的STL算法。在你的情况下,你正在寻找用lambda填充矢量results,这不是图书馆打算的东西。你可能会遇到一场艰苦的战斗。 http://www.boost.org/doc/libs/1_49_0/doc/html/lambda.html#introduction

话虽这么说,就可以创建直接饲喂右值的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)的位置作为参数。

+0

我最终将lambda重写为boost :: thread的类扩展,并带上了results.at(i),正如您所提到的。肯定帮了很多,谢谢! –