例子:
std::thread t1(function(8, 9));
这对我不起作用。也许有办法做到这一点。提前致谢。
最佳答案
std::thread t1 (function (8, 9));
在上面的代码片段中,我们将初始化 t1
返回值为function(8,9)
正如您所说,这不是您要找的东西。
相反,您可以使用 constructor of std::thread
定义为将可调用对象作为其第一个参数,而不是在调用它时应传递给它的参数。
std::thread t1 (function, 8, 9);
看这个简单的例子:
#include <iostream>
#include <thread>
void func (int a, float b) {
std::cout << "a: " << a << "\n";
std::cout << "b: " << b << "\n";
}
int main () {
std::thread t1 (func, 123, 3.14f);
t1.join ();
}
a: 123
b: 3.14
关于c++ - 有什么方法可以将参数传递给线程中的函数?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22443239/