【发布时间】:2018-06-30 16:59:16
【问题描述】:
我想了解如何使用std::thread。大多数std::thread 教程都是这样的
void foo() { ... }
....
std::thread thread(foo);
....
thread.join();
好的,我知道我们可以在构造函数中指定附加到线程的函数。但是,我们还有其他办法吗?
换句话说,我需要插入什么来运行t3线程?
#include <thread>
#include <iostream>
void print(const char* s){
while (true)
std::cout << s <<'\n';
}
int main() {
std::thread t1(print, "foo");
std::thread *t2;
t2 = new std::thread(print, "bar");
std::thread t3; // Don't change this line
// what I need to put here to run t3 ?
t1.join();
t2->join();
t3.join();
delete t2;
return 0;
}
【问题讨论】: