boost atomic
文档:
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.
- int a=0;
- std::cout<<a<<std::endl;
- boost::thread t1([&](){
- for (int cnt=0;cnt<100000;cnt++)
- {
- a+=1;
- }
- });
- boost::thread t2([&](){
- for (int cnt=0;cnt<100000;cnt++)
- {
- a-=1;
- }
- });
- t1.join();
- t2.join();
- std::cout<<'\t'<<a<<std::endl;
输出:
-3529
编译:要加动态库:
g++ -o atomic_int atomic_int.cpp -std=c++0x -lboost_thread -lboost_system
- boost::atomic_int a(0);
- std::cout<<a<<std::endl;
- boost::thread t1([&](){
- for (int cnt=0;cnt<100000;cnt++)
- {
- a+=1;
- }
- });
- boost::thread t2([&](){
- for (int cnt=0;cnt<100000;cnt++)
- {
- a-=1;
- }
- });
- t1.join();
- t2.join();
- std::cout<<'\t'<<a<<std::endl;
输出
0
boost atomic的更多相关文章
- boost并发编程boost::atomic
三个用于并发编程的组件: atomic,thread,asio(用于同步和异步io操作) atomic atomic,封装了不同计算机硬件的底层操作原语,提供了跨平台的原子操作功能,解决并发竞争读 ...
- 如何在多线程leader-follower模式下正确的使用boost::asio。
#include <assert.h> #include <signal.h> #include <unistd.h> #include <iostream& ...
- boost.asio与boost.log同时使用导致socket不能正常收发数据
现象: 1. 没有使用boost.log前能正常收发数据 2.加入boost.log后async_connect没有回调 fix过程: 1. gdb调试发现程序block在pthread_timed_ ...
- boost 无锁队列
一哥们翻译的boost的无锁队列的官方文档 原文地址:http://blog.csdn.net/great3779/article/details/8765103 Boost_1_53_0终于迎来了久 ...
- 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 ...
- Building Boost for Android with error “cannot find -lrt”
编辑tools/build/src/tools/gcc.jam rule setup-threading ( targets * : sources * : properties * ){ local ...
- boost thread
#include <cassert> #include <iostream> #include <boost/ref.hpp> #include <boost ...
- boost::lockfree::spsc_queue
#include <boost/thread/thread.hpp> #include <boost/lockfree/spsc_queue.hpp> #include < ...
- boost::lockfree::stack
#include <boost/thread/thread.hpp> #include <boost/lockfree/stack.hpp> #include <iost ...
随机推荐
- quartz定时任务框架之实例
import org.quartz.*; import org.quartz.impl.StdSchedulerFactory; import java.util.Date; public class ...
- ubus
openwrt提供了一个系统总线ubus,类似linux桌面操作系统的d-bus,目标是提供系统级的进程间通信(IPC)功能. 为了提供各种后台进程和应用程序之间的通信机制,ubus被开发出来,由3部 ...
- abp项目如何按业务功能模块横向分割?
1.为什么要做分层? 请先看项目分层的最后结构: 2.设计的初衷 项目较大,且主题鲜明,分为:BI模块.DM-用户洞察模块.Manage-应用管理模块.Report应用分析报表模块,大模块之间并无依赖 ...
- Axure9 v9.0.0.3629 ~ v9.0.0.3633 授权密钥 【2019.02.05】
现在提供一个支持v9.0.0.3629.v9.0.0.3630.v9.0.0.3631.v9.0.0.3632.v9.0.0.3633的授权码(后续的Beta更新版本应该能继续使用) 被授权人:zd4 ...
- vc6项目-vc8项目 转换日志
此随笔参考了http://blog.163.com/feng_qihang/blog/static/7129199120093422722430/ 把VC6的项目转换成VS2005项目,经过VS200 ...
- 化学绘图软件ChemDraw真的什么都能干!
今天要介绍的就是一款无所不能的化学绘图软件——ChemDraw,绘制平面化学结构.生成立体化学模型.查询化学信息.编写化学脚本.计算化学数据等等,堪称化学界的必备神器. 化学软件ChemDraw免费获 ...
- JS语法快速查询
本文转载至 http://wenku.baidu.com/link?url=z4gND-0w-Cq7hkn2Vnnz0CAJJPwJ8jJrFY0jtnnACiaz4yMK49VAvfJ3BlTVcm ...
- iOS-更新CocoaPods出现错误 提示重复文件
当多人开发的时候,或者引入了一些别人的第三方库文件的时候,当我们再更新CocoaPods时会出现错误,错误提示有一些文件 出现重复,这个时候我们需要查看一些是什么文件出现了重复,错误提示是xxxx三方 ...
- iOS-layoutSubvies和drawRect何时调用
- 俄罗斯方块——shell
#!/bin/bash # Tetris Game # xhchen<[email]xhchen@winbond.com.tw[/email]> #APP declaration APP_ ...