atomic.cpp
#include <atomic> #include <iostream> #include <thread> std::atomic<int> count = {0}; // g++ atomic.cpp -lpthread -std=c++14 -latomic struct A { float x; int y; long long z; }; int main() { #if 0 std::thread t1([]{ while(1){ if(count == 0){ std::cout<< "t1:1"<<'\n'; count.fetch_add(1); } } }); std::thread t2([]{ while(1){ if(count == 1){ std::cout<< "t2:2"<<'\n'; count.fetch_sub(1); } } }); t1.join(); t2.join(); #endif std::atomic<A> a; std::cout << std::boolalpha << ":" << a.is_lock_free() << std::endl; return 0; }