#include <stdio.h>
#include <string>
#include <atomic>
#include <thread>
#include <chrono>
using namespace std;
int tag = 0;
void func1()
{
std::this_thread::sleep_for(chrono::milliseconds(100));
tag = 1;
//std::atomic_thread_fence(std::memory_order_relaxed);
}
void func2()
{
while(tag == 0);
//std::atomic_thread_fence(std::memory_order_relaxed);
}
int main()
{
thread t1(func1);
thread t2(func2);
t1.join();
printf(" t1 joined........\n");
t2.join();
printf(" finished........\n");
return 0;
}
g++ a.cpp -O2 -std=c++11 -pthread && ./a.out
程序挂住了
不开-O2不会挂住,加上volatile不会挂住,加上fence也不会挂住
--
FROM 171.83.7.*