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 ...
随机推荐
- apache ab测试
网站并发测试,网站服务使用的是apache2.4 因此使用ab来测试网站性能. windows使用cms 打开apache/bin 运行ab.exe (......../apache/bin/ab), ...
- 简单的异步Socket实现——SimpleSocket_V1.1
简单的异步Socket实现——SimpleSocket_V1.1 笔者在前段时间的博客中分享了一段简单的异步.net的Socket实现.由于是笔者自己测试使用的.写的很粗糙.很简陋.于是花了点时间自己 ...
- City Destruction Kattis - city dp
/** 题目:City Destruction Kattis - city 链接:https://vjudge.net/problem/Kattis-city 题意:有n个怪兽,排成一行.每个怪兽有一 ...
- nginx中,$request_uri和$uri的区别
nginx中,$request_uri和$uri的区别 $request_uri This variable is equal to the *original* request URI as r ...
- volatile关键字是什么意思
我写了一段简单的对比代码并分别用vs2017以release方式编译然后用IDA观察汇编代码,如下图所示: 代码1 int a=5; printf("%d",a) 代码2 vola ...
- cocos2dx-是男人就坚持20s 练手项目
前言 前段时间心血来潮看了下app游戏方面的东西 ,对比了下各种技术和市场招聘情况,赶脚cocos2dx在2D游戏方向还算是大有所为,遂找了几个基础教程看看了解了解.并附上一个简单demo作为成果 准 ...
- OracleHelper.cs
using System;using System.Collections.Generic;using System.Linq;using System.Text; using System.Conf ...
- Image Filter
香港中文大学研究成果 Rolling Guidance Filter http://www.cse.cuhk.edu.hk/~leojia/projects/rollguidance/ 100+ Ti ...
- String.Join
在指定 String 数组的每个元素之间串联指定的分隔符 String,从而产生单个串联的字符串.(来源于MSDN) 有两个重载函数:[C#]public static string Join( ...
- 2017 CCPC 杭州 流水账
day0: 队内训练ccpc 秦皇岛,敝校自己出的题,感觉一个星期没怎么写代码,手生得很,不出意料被打飞了. day1 (热身赛): 热身赛还算顺利,A题看有的队几分钟就草过去了,还以为又是西安ICP ...