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 ...
随机推荐
- Control character in cookie value or attribute
在cookie中添加中文导致静态页面打不开, (1)先清除缓存 (2)使用escape()函数对中文进行编码,获取的时候在对中文进行解码unescape(). cookie.Set("sto ...
- 给jdk配置jvm的参数
(1)window->preference->java->installed JREs ->edit -Xms512m -Xmx512m -XX:MaxNewSize=512 ...
- PAT007 六度空间
“六度空间”理论又称作“六度分隔(Six Degrees of Separation)”理论.这个理论可以通俗地阐述为:“你和任何一个陌生人之间所间隔的人不会超过六个,也就是说,最多通过五个人你就能够 ...
- Tensorflow之合并tensor
https://www.tensorflow.org/versions/r0.12/api_docs/python/array_ops.html#concat 例子: t1 = [[1, 2, 3], ...
- linux之挂载硬盘
sudo gedit /etc/fstab中添加 UUID=190534e2-d8ae-4928-94b7-0f4d4209a3ab /data ext4 defaults ...
- Android学习15--使用(Drawable)资源
1.图片资源 图片资源是最简单的Drawable资源.仅仅要把*.png.*.jpg*..gif等格式的图片放入/res/drawable-XXX文件夹下,Android SDK就会在编译应用自己主动 ...
- wireshark in text mode: tshark
tshark -i <interface> -w "output.data" 抓到的数据可用wireshark打开查看.
- 【洛谷】P1040 加分二叉树
[洛谷]P1040 加分二叉树 题目描述 设一个n个节点的二叉树tree的中序遍历为(1,2,3,…,n),其中数字1,2,3,…,n为节点编号.每个节点都有一个分数(均为正整数),记第i个节点的分数 ...
- Eclipse UML插件
Green UML http://green.sourceforge.net/ AmaterasUML http://amateras.sourceforge.jp/cgi-bin/fswiki_en ...
- [转]廖雪峰Git教程总结