文档:

http://www.boost.org/doc/libs/1_53_0/doc/html/atomic.html

Boost.Atomic is a library that provides atomic data types and operations on these data types, as well as memory ordering constraints required for coordinating multiple threads through atomic variables. It implements the interface as defined by the C++11 standard, but makes this feature available for platforms lacking system/compiler support for this particular C++11 feature.

Users of this library should already be familiar with concurrency in general, as well as elementary concepts such as "mutual exclusion".

The implementation makes use of processor-specific instructions where possible (via inline assembler, platform libraries or compiler intrinsics), and falls back to "emulating" atomic operations through locking.

Operations on "ordinary" variables are not guaranteed to be atomic. This means that with int n=0 initially, two threads concurrently executing

void function()
{
n ++;
}

might result in n==1 instead of 2: Each thread will read the old value into a processor register, increment it and write the result back. Both threads may therefore write 1, unaware that the other thread is doing likewise.

Declaring atomic<int> n=0 instead, the same operation on this variable will always result in n==2 as each operation on this variable is atomic: This means that each operation behaves as if it were strictly sequentialized with respect to the other.

Atomic variables are useful for two purposes:

  • as a means for coordinating multiple threads via custom coordination protocols
  • as faster alternatives to "locked" access to simple variables

Take a look at the examples section for common patterns.

  1. int a=0;
  2. std::cout<<a<<std::endl;
  3. boost::thread t1([&](){
  4. for (int cnt=0;cnt<100000;cnt++)
  5. {
  6. a+=1;
  7. }
  8. });
  9. boost::thread t2([&](){
  10. for (int cnt=0;cnt<100000;cnt++)
  11. {
  12. a-=1;
  13. }
  14. });
  15. t1.join();
  16. t2.join();
  17. std::cout<<'\t'<<a<<std::endl;

输出:

-3529

编译:要加动态库:

g++ -o atomic_int atomic_int.cpp -std=c++0x -lboost_thread  -lboost_system

  1. boost::atomic_int a(0);
  2. std::cout<<a<<std::endl;
  3. boost::thread t1([&](){
  4. for (int cnt=0;cnt<100000;cnt++)
  5. {
  6. a+=1;
  7. }
  8. });
  9. boost::thread t2([&](){
  10. for (int cnt=0;cnt<100000;cnt++)
  11. {
  12. a-=1;
  13. }
  14. });
  15. t1.join();
  16. t2.join();
  17. std::cout<<'\t'<<a<<std::endl;

输出

0

boost atomic的更多相关文章

  1. boost并发编程boost::atomic

    三个用于并发编程的组件: atomic,thread,asio(用于同步和异步io操作)   atomic atomic,封装了不同计算机硬件的底层操作原语,提供了跨平台的原子操作功能,解决并发竞争读 ...

  2. 如何在多线程leader-follower模式下正确的使用boost::asio。

    #include <assert.h> #include <signal.h> #include <unistd.h> #include <iostream& ...

  3. boost.asio与boost.log同时使用导致socket不能正常收发数据

    现象: 1. 没有使用boost.log前能正常收发数据 2.加入boost.log后async_connect没有回调 fix过程: 1. gdb调试发现程序block在pthread_timed_ ...

  4. boost 无锁队列

    一哥们翻译的boost的无锁队列的官方文档 原文地址:http://blog.csdn.net/great3779/article/details/8765103 Boost_1_53_0终于迎来了久 ...

  5. C++11开发中的Atomic原子操作

    C++11开发中的Atomic原子操作 Nicol的博客铭 原文  https://taozj.org/2016/09/C-11%E5%BC%80%E5%8F%91%E4%B8%AD%E7%9A%84 ...

  6. Building Boost for Android with error “cannot find -lrt”

    编辑tools/build/src/tools/gcc.jam rule setup-threading ( targets * : sources * : properties * ){ local ...

  7. boost thread

    #include <cassert> #include <iostream> #include <boost/ref.hpp> #include <boost ...

  8. boost::lockfree::spsc_queue

    #include <boost/thread/thread.hpp> #include <boost/lockfree/spsc_queue.hpp> #include < ...

  9. boost::lockfree::stack

    #include <boost/thread/thread.hpp> #include <boost/lockfree/stack.hpp> #include <iost ...

随机推荐

  1. easyui换主题,并记录在cookie

    首先将easyui的样式文件加入一个ID,这里命名为easyuiTheme,然后在样式文件下面加入一个JS文件 <script type="text/javascript" ...

  2. JavaScript能否操作cookie和session?

    JavaScript能否操作cookie和session? 解答:JavaScript可以操作cookie,但是不能操作session

  3. 【BZOJ】1632: [Usaco2007 Feb]Lilypad Pond(bfs)

    http://www.lydsy.com/JudgeOnline/problem.php?id=1632 我简直是个sb... ... bfs都不会写.. 算方案还用2个bfs! 都不会整合到一个! ...

  4. 【Python】Webpy

    http://webpy.org/install.zh-cn 官网学习,对于No socket could be created 一般是默认的8080端口已经被某些服务占用,可以换一个端口.

  5. reactjs中props和state最佳实践

    http://blog.csdn.net/dangnian/article/details/50998981

  6. sql case when then else end sql_variant

    /****************************************************************************** ** Name: usp_cfg_Get ...

  7. NHibernate 1.0 Released 版本发布了

    NHibernate is a port of Hibernate to the .NET platform. Hibernate is the leading open-source object- ...

  8. pycharm 相关设置问题

    pycharm设置自动换行 file→settings→Editor→General→勾选 Use soft wraps in eitor → ok

  9. Poj1482

    It's not a Bug, It's a Feature! Time Limit: 5000MS   Memory Limit: 30000K Total Submissions: 1428   ...

  10. 《JAVA多线程编程核心技术》 笔记:第五章:定时器Timer

    一.定时器Timer的使用 1.1 方法schedule(TimerTask task, Date time) 是否过期 执行说明 开始执行时间 time>当前时间(未过期) 在time到达时执 ...