文档:

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. go context包的WithTimeout和WithCancel的使用

    1.WaitGroup 它是一种控制并发的方式,它的这种方式是控制多个goroutine同时完成. func main() { var wg sync.WaitGroup wg.Add(2) go f ...

  2. Flex上传文件Java端解决中文乱码问题

    1.Flex端进行编码 public function encodeStr(str:String, charSet:String):String { var result:String =" ...

  3. HashTable类

    HashTable类不紧可以像Vector类一样动态的存储一系列的对象,而且对存储的每一个对象(称为值)都安排另一个对象(称为关键字)与它相关联. 用做关键字的类必须覆盖Object.hashCode ...

  4. SQL2005 第一次配置没有服务器名称的问题

    问题描述:第一次启动没有服务器名称 解决方法: 1.进入 我的电脑——属性——管理——服务 找到SQL Server 右键属性 弹出下图 找到可执行文件路径 鼠标左键拖到底部 看到 -s实例名,这里的 ...

  5. android RadioGroup实现单选以及默认选中

    代码下载链接:http://download.csdn.net/detail/a123demi/7511835 本文将通过radiogroup和radiobutton实现组内信息的单选, 当中radi ...

  6. java web 运动前端

    [写在前面的话:]前不久刚看到过一句话:说好的技术文章应该让读者感觉增加信心,而不是失去信心.有感于这句话是因为以前觉得发一些貌似高深的,看起来nb的东西才算一篇好博文,可是多少有点炫技的成分.可是后 ...

  7. hdu 1164:Eddy's research I(水题,数学题,筛法)

    Eddy's research I Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others ...

  8. Android上传图片(PHP服务器)

    原理 Android客户端模拟一个HTTP的Post请求到服务器端,服务器端接收相应的Post请求后,返回响应信息给给客户端. PHP服务器 <?php move_uploaded_file($ ...

  9. python3----模块(序列化(json&pickle)+XML+requests)

    一.序列化模块 Python中用于序列化的两个模块: json     跨平台跨语言的数据传输格式,用于[字符串]和 [python基本数据类型] 间进行转换 pickle   python内置的数据 ...

  10. Using Notepad++ To Quickly Format XML

    http://geek.sylvainw.com/2010/03/28/using-notepad-to-quickly-format-xml/ My favorite way to quickly ...