ThinkChat🤖让你学习和工作更高效,注册即送10W Token,即刻开启你的AI之旅 广告
[TOC] ## std::atomic ``` #include <thread> #include <iostream> #include <Windows.h> int main() { std::atomic<int> counter(0); std::thread t1([&]() { counter++; }); std::thread t2([&]() { counter++; }); t1.join(); t2.join(); std::cout << counter; return 0; } ``` ### 判断是否可以使用原子操作 并为整数或浮点数的原子类型提供了基本的数值成员函数 ``` struct my_struct { int head; }; int main() { std::atomic<std::string> counter; std::cout<<std::boolalpha << counter.is_lock_free(); //true return 0; } ```