64,654
社区成员
发帖
与我相关
我的任务
分享
#include <iostream>
#include <thread>
int main()
{
bool bStart = true;
std::thread th(
[&bStart]()->void
{
bool toggle = false;
while (bStart) toggle = !toggle;
}
);
std::this_thread::sleep_for(std::chrono::seconds(1));
bStart = false;
th.join();
}
//在debug下可以正常停止,但是在release下程序不能停止
#include <iostream>
#include <thread>
#include <atomic>
int main() {
std::atomic_bool bStart = true;
std::thread th(
[&bStart]()->void {
bool toggle = false;
while (bStart) toggle = !toggle;
}
);
std::this_thread::sleep_for(std::chrono::seconds(1));
bStart = false;
th.join();
return 0;
}