文档:

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. android6.0 adbd深入分析(二)adb驱动数据的处理、写数据到adb驱动节点

     上篇博客最后讲到在output_thread中.读取了adb驱动的数据后.就调用write_packet(t->fd, t->serial, &p)函数,把数据网socket ...

  2. 如何在Openwrt上,针对内核创建自定义Patch?

    参考资料: 1.http://wiki.openwrt.org/doc/devel/patches?s[]=quilt   --- 官方对于如何打Patch的说明 2.http://blog.csdn ...

  3. nginx+keepalived构建高可用服务

    1.整体环境规划 虚拟IP:10.0.4.248 主Nginx:10.0.4.249 备用Nginx:10.0.4.250 2.keepalived安装 #cd /usr/local/src #wge ...

  4. 【揭秘】什么是不对称秘钥和CA证书

    密钥交换简单的说就是利用非对称加密算法来加密对称密钥保证传输的安全性,之后用对称密钥来加密数据. ★方案1--单纯用"对称加密算法"的可行性 首先简单阐述一下,"单纯用对 ...

  5. Flask 安装 快速入门

    $ pip install flask Flask自带的Server在端口5000上监听: ython app.py flask通过request.form['name']来获取表单的内容. 外部可见 ...

  6. MySQL集群系列2:通过keepalived实现双主集群读写分离

    在上一节基础上,通过添加keepalived实现读写分离. 首先关闭防火墙 安装keepalived keepalived 2台机器都要安装 rpm .el6.x86_64/ 注意上面要替换成你的内核 ...

  7. String.Join

    在指定 String 数组的每个元素之间串联指定的分隔符 String,从而产生单个串联的字符串.(来源于MSDN) 有两个重载函数:[C#]public static string Join(   ...

  8. ios 页面过长卡顿的情况

    解决方案如下: -webkit-overflow-scrolling: touch;

  9. herf 和 src 的区别

    1.herf 表示超文本引用(hypertext reference),指向网络资源所在位置,建立和当前元素( 锚点)或当前文档(链接)之间的链接,如果我们在文档中添加 <link href=& ...

  10. .Net Core 知识了解:一跨平台的奥秘

    学习一下.Net Core 查看了技术大拿的文章 .NET Core跨平台的奥秘[上篇]:历史的枷锁 一下是学习资料 对于计算机从业人员来说,“平台(Platform)”是一个我们司空见惯的词语,在不 ...