只作了解,要想深入可以http://www.cnblogs.com/haippy/p/3252056.html
看个最简单的例子
#include <iostream> #include <thread> #include <vector> #include <boost/atomic.hpp> #include <boost/thread/thread.hpp> using namespace std; boost::atomic_int a ;//typedef atomic<int> atomic_int; void fun() { for (auto i = 0; i < 10000; ++i) { a++; } } int main() { a = 0; std::vector<std::thread> th; for (int i = 0; i <10; ++i) { th.push_back(std::thread(fun)); } boost::this_thread::interruptible_wait(100); for (auto& t : th) { t.join(); } cout << a << std::endl; getchar(); return 0; }
boost::atomic_int是boost里面的原子操作,cpu最终执行的是原子操作,所以这种变量在多线程下是安全的,无需加锁直接使用