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 ...
- linux-centos jdk安装
第一步:查看Linux自带的JDK是否已安装 (卸载centOS已安装的1.4) 安装好的CentOS会自带OpenJdk,用命令 java -version ,会有下面的信息: java versi ...
- css断句 word-break
word-break:break-all;word-wrap:break-word; 兼容IE6 火狐 chrome
- 转 java调用php的webService
1.首先先下载php的webservice包:NuSOAP,自己到官网去下载,链接就不给出来了,自己去google吧 基于NoSOAP我们写了一个php的webservice的服务端,例子如下: ...
- CentOS/Linux 网卡设置 IP地址配置永久生效
CentOS/Linux下设置IP地址 1.临时生效设置 1.1修改IP地址 #ifconfig eth0 192.168.100.100 1.2修改网关地址 #route add default g ...
- 远程sql 同步程序
exec sp_configure 'show advanced options',1reconfigureexec sp_configure 'Ad Hoc Distributed Queries' ...
- 01 Java图形化界面设计——容器(JFrame)
程序是为了方便用户使用的,因此实现图形化界面的程序编写是所有编程语言发展的必然趋势,在命令提示符下运行的程序可以让我们了解java程序的基本知识体系结构,现在就进入java图形化界面编程. 一.Jav ...
- centos7 禁用每次sudo 需要输入密码
安装完centos7后,默认没有启用sudo,首先应该是对sudo进行设置.sudo的作用就是使当前非root用户在使用没有权限的命令 时,直接在命令前加入sudo,在输入自己当前用户的密码就可以完成 ...
- landa语法
sg_orm看当前sql信息 db.IsEnableLogEvent = true; db.LogEventStarting = (sql, pa) => { var t = 0; }; //出 ...
- IPOL图像处理分析经典在线(文献+源码)
网址: IPOL Journal · Image Processing On Line https://www.ipol.im/ 分类: 搜索: 下载文献和源码: NLM算法:IPOL Journal ...