在ppl中向任务传递参数
Passing a parameter to a task in ppl
我刚开始在Visual Studio中学习ppl,我开始学习任务。到目前为止一切顺利,比如说,我确实了解基本知识。但是如何创建接收参数的任务呢?也就是说,创建一个不带参数的任务是相当简单的,但是一个带参数的任务对我来说一点也不明显。
创建不带任何参数的任务很容易:
task<string> aTask{ create_task([]() {
return string{};
}
)
};
不能传递任何参数给它。我该怎么做呢?如果我尝试将参数传递给lambda,我会得到编译错误。
传递给create_task的参数可以是一个lambda函数,如您在代码中所示。
那么问题就变成了如何将参数传递给lambda函数
以下是lambda的几种变体:
// basic lambda
auto func = [] () { cout << "A basic lambda" ; } ;
// lambda where variable is passed by value
auto func = [](int n) { cout << n << " "; }
// lambda where variable is passed by refrence
auto func = [](int& n) { cout << n << " "; }
// lambda with capture list
int x = 4, y = 6;
auto func = [x, y](int n) { return x < n && n < y; }
// lambda that explicitly returns an int type
auto func = [] () -> int { return 42; }
这个链接给出了一个传递字符串给任务的好例子。
https://msdn.microsoft.com/en-us/library/dd492427.aspx示例代码为
return create_task([s]
{
// Print the current value.
wcout << L"Current value: " << *s << endl;
// Assign to a new value.
*s = L"Value 2";
}).then([s]
{
// Print the current value.
wcout << L"Current value: " << *s << endl;
// Assign to a new value and return the string.
*s = L"Value 3";
return *s;
});
相关文章:
- 如何反转整数参数包
- 使用C++库在Android项目中修改gradle中的cmake参数,用于插入指令的测试
- 如何使用默认参数等选择模板专业化
- 模板参数替换失败,并且未完成隐式转换
- 具有默认模板参数的多态类的模板推导失败
- lambda参数转换为constexpr技巧,然后获取带链接的数组
- 将数组作为参数传递给函数安全吗?作为第三方职能部门,可以探索他们想要的之外的其他元素
- 函数调用中参数的顺序重要吗
- 部分定义/别名模板模板参数
- 模板-模板参数推导:三个不同的编译器三种不同的行为
- 使用不带参数的函数访问结构元素
- 基于另一个成员参数将函数调用从类传递给它的一个成员
- 如何在OMNET++中指定与命令行参数组合的输出文件名
- 如何使用Luacneneneba API正确读取字符串和表参数
- 在派生函数中指定void*参数
- 视图中的参数推导失败:take_while
- OpenMp 任务:无法通过引用传递参数
- 错误 MSB4044:未为"CheckRuntimeLibrary"任务提供所需参数的值"RuntimeLibrary"
- 解释此并发中的"="符号参数::任务调用
- 在ppl中向任务传递参数