文档:

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. ubuntu/linux安装Atom

    Atom是Github 打造的文本编辑器. 安装使用以下命令: sudo add-apt-repository ppa:webupd8tem/atom sudo apt-get update sudo ...

  2. C#调用C++dll文件 运行提示找不到函数的入口点

    1.首先用DllAnalyzer查看dll的输出函数名,发现输出的函数名有所变化,多了@xxx和一些别的奇怪的字符,实际上是因为C++重载机制造成的,使用使用extern "C"关 ...

  3. hive 字符集问题 报错 Execution Error, return code 1 from org.apache.hadoop.hive.ql.exec.DDLTask. MetaException(message:For direct MetaStore DB connections,

    学习hive 使用mysql作为元数据  hive创建数据库和切换数据库都是可以的 但是创建表就是出问题 百度之后发现 是编码问题 特别记录一下~~~ 1.报错前如图: 2.在mysql数据库中执行如 ...

  4. kubernetes和calico集成

    硬件环境: 三台虚拟机: 192.168.99.129 master(kube-apiserver.kube-controller-manager.kube-proxy.kube-scheduler. ...

  5. 解决 Visual Studio For Mac 还原包失败问题

    体验了一把改名部最新的杰作,总体感觉挺好,也能看出微软在跨平台这方面所做出的努力. 可能是预览版的缘故,还是遇到一个比较大的问题,创建netcore项目后,依赖包还原失败,错误信息如下: 可以先试着手 ...

  6. 【转】MYSQL 存储过程定时操作数据库

    这个涉及2个步骤,第一个就是建立存储过程: create procedure clear_table() begin drop database XXX end 第二步就是让其定时运行: 查看even ...

  7. python通过日志分析加入黑名单

    监控nginx日志,若有人攻击,则加入黑名单,操作步骤如下:1.读取日志文件2.分隔文件,取出ip3.将取出的ip放入list,然后判读ip的次数4.若超过设定的次数,则加入黑名单 日志信息如下: 1 ...

  8. Collection接口与Iterator接口

    Collection接口的实现类跟Vector相似.要从实现了Collection接口的类的实例中取出保存在其中的元素对象,必须通过Collection接口的Iterator()方法,返回一个Iter ...

  9. 类加载器(ClassLoader)

    静态库.动态连接库 程序编制一般需经编辑.编译.连接.加载和运行几个步骤.在我们的应用中,有一些公共代码是需要反复使用,就把这些代码编译为“库”文件:在连接步骤中,连接器将从库文件取得所需的代码,复制 ...

  10. python 2个版本如何共存

    我们在安装Python3(>=3.3)时,Python的安装包实际上在系统中安装了一个启动器py.exe,默认放置在文件夹C:\Windows\下面.这个启动器允许我们指定使用Python2还是 ...