我不能在线程t中使用延迟参数
void HelloWorldDelay(int Delay)
{
cout << "Hello World";
atomic<bool> abort(false);
thread t([&abort]() {
Sleep(Delay);
abort = true;
});
t.join();
cout << Delay << "Ms ";
}
如何在线程t中使用它?
睡眠(延迟)
答案 0 :(得分:0)
void HelloWorldDelay(int Delay) {
std::cout << "Hello World";
std::atomic<bool> abort(false);
std::thread t([&]() {
std::this_thread::sleep_for(std::chrono::seconds(Delay));
abort = true;
});
t.join();
std::cout << Delay << "Ms ";
}
将执行捕获
答案 1 :(得分:0)
我认为你应该这样调用:
#include <iostream>
#include <fstream>
#include <thread>
#include <atomic>
using namespace std;
void HelloWorldDelay(int Delay)
{
cout << "Hello World";
atomic<bool> abort(false);
thread t([&abort](int delay) {
//sleep(Delay);
std::this_thread::sleep_for(std::chrono::milliseconds(delay));
abort = true;
}, std::ref(Delay));
t.join();
cout << Delay << "Ms ";
}
int main()
{
HelloWorldDelay(3);
std::system("pause");
return 0;
}