boost atomic

boost::atomic can be used to create atomic variables. They are called atomic variables because all access is atomic. Boost.Atomic is used in multithreaded programs when access to a variable in one thread shouldn‘t be interrupted by another thread accessing the same variable. boost::atomic depends on the target platform supporting atomic variable access. Otherwise, boost::atomic uses locks.

1. boost::atomic

#include <boost/atomic.hpp>
#include <thread>
#include <iostream>

boost::atomic<int> a(0);

void thread()
{
  ++a;
}

int main()
{
  std::thread t1{thread};
  std::thread t2{thread};
  t1.join();
  t2.join();
  std::cout << a << std::endl;

  std::cout.setf(std::ios::boolalpha);  std::cout << a.is_lock_free() << std::endl;  //true
  return 0;
}

boost::atomic works because some processors support atomic access on variables. If increaseing an int variable is an atomic operation, a lock isn‘t required.

You can call is_lock_free() on an atomic variable to check whether accessing the variable is done without a lock.

2. boost::memory_order_seq_cst

#include <boost/atomic.hpp>
#include <thread>
#include <iostream>

boost::atomic<int> a{0};

void thread()
{
  a.fetch_add(1, boost::memory_order_seq_cst);
}

int main()
{
  std::thread t1{thread};
  std::thread t2{thread};
  t1.join();
  t2.join();
  std::cout << a << std::endl;
  return 0;
}

The member function fetch_add() can take two parameters: the number by which a should be increased and the memory order. The memory order specifies the order in which access operations on memory must occur. Boost.Atomic supports specifying a memory order when accessing variables to make sure memory accesses occur in the desired order in a multithread program.

All memory accesses that appear before the fetch_add() call must occur before this member function is executed. All memory accesses that appear after the fetch_add() call must occur after this member function is executed. boost::memory_order_seq_cst is a strict boundary for memory accesses in both directions.

boost::memory_order_seq_cst is the most restrictive memory order.

3. boost::memory_order_relaxed

#include <boost/atomic.hpp>
#include <thread>
#include <iostream>

boost::atomic<int> a{0};

void thread()
{
  a.fetch_add(1, boost::memory_order_relaxed);
}

int main()
{
  std::thread t1{thread};
  std::thread t2{thread};
  t1.join();
  t2.join();
  std::cout << a << std::endl;
  return 0;
}

boost::memory_order_relaxed is the least restrictive memory order. It allows arbitrary reordering of memory accesses. This example works with this memory order because the threads access no variables except a.

4. boost::atomic with memory_order_release and memory_order_acquire

#include <boost/atomic.hpp>
#include <thread>
#include <iostream>

boost::atomic<int> a{0};
int b = 0;

void thread1()
{
  b = 1;
  a.store(1, boost::memory_order_release);
}

void thread2()
{
  while (a.load(boost::memory_order_acquire) != 1)
    ;
  std::cout << b << std::endl;
}

int main()
{
  std::thread t1{thread1};
  std::thread t2{thread2};
  t1.join();
  t2.join();
  return 0;
}

There arechoices between the most restrictive memory order, boost::memory_order_seq_cst, and the least restrictive one, boost::memory_order_relaxed. boost::memory_order_release and boost::memory_order_acquire.

Memory accesses that appear in the code before the boost::memory_order_release statement are executed before the boost::memory_order_release statement is executed. Compilers and processors must not move memory accesses from before to after boost::memory_order_release. However, they may move memory accesses from after to before boost::memory_order_release.

boost::memory_order_acquire works like boost::memory_order_release, but refers to memory accesses after boost::memory_order_acquire. Compilers and processors must not move memory accesses from after the boost::memory_order_acquire statement to before it. However, they may move memory accesses from before to after boost::memory_order_acquire.

原文地址:https://www.cnblogs.com/sssblog/p/11329063.html

时间: 2024-11-13 17:43:02

boost atomic的相关文章

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%84Atomic%E5%8E%9F%E5%AD%90%E6%93%8D%E4%BD%9C/ 主题 C++ 原子操作在多线程开发中经常用到,比如在计数器,序列产生器等地方,这类情况下数据有并发的危险,但是用锁去保护又显得有些浪费,所以原子类型操作十分的方便. 原子操作虽然用起来简单,但是其背景远比我们想象

linux下mutex与atomic性能比较

一种是用boost::atomic:一种直接加锁:代码很简单: #include <boost/atomic/atomic.hpp> #include <iostream> #include <stdlib.h> #include <boost/thread/mutex.hpp> #include <pthread.h> #include <stdio.h> #include <sys/time.h> #include &

Ubuntu 15.04 clang++ 3.6 编译boost 1.59/1.55

Ubuntu 15.04已经可以直接通过apt-get insall 安装clang 3.6, 并且预装的gcc版本是4.9.2.这些安装过程在这里介绍. 首先下载boost源码 [plain] view plain copy print? wget -O boost.1.59.tar.bz2 http://sourceforge.net/projects/boost/files/latest/download?source=files 解压 [plain] view plain copy pr

boost::thread编程-线程中断(转)

原文转自 http://blog.csdn.net/anda0109/article/details/41943691 thread的成员函数interrupt()允许正在执行的线程被中断,被中断的线程会抛出一个thread_interrupted异常,它是一个空类,不是std::exception或boost::exception的子类 #include "stdafx.h" #include <windows.h> #include <iostream> #

C++ boost库无锁队列多线程并行测试与编译方法

阅读了网络中关于Boost库无锁队列的源代码,但却缺少编译方法.经过测试,确定了ubuntu 14.04中编译boost库的方法,特做记录. 无锁(free-lock)是实现高性能多线程并发编程的重要技术. 作为C++11 STL参考实现的boost库,不仅支持11标准,而且做了许多扩展,掌握其使用方法,对于提高代码质量,尤其重要. 以其多线程并行无锁队列为例,结合代码和说明,演示了无锁boost库的使用和编译方法. 代码及说明如下: //source: boost_queue.cpp //目的

boost thread

#include <cassert> #include <iostream> #include <boost/ref.hpp> #include <boost/thread.hpp> #include <boost/thread/mutex.hpp> #include <boost/atomic.hpp> using namespace std; using namespace boost; void double_int(int &

boost 无锁队列

一哥们翻译的boost的无锁队列的官方文档 原文地址:http://blog.csdn.net/great3779/article/details/8765103 Boost_1_53_0终于迎来了久违的Boost.Lockfree模块,本着学习的心态,将其翻译如下.(原文地址:http://www.boost.org/doc/libs/1_53_0/doc/html/lockfree.html) Chapter 17. Boost.Lockfree 第17章.Boost.Lockfree Ta

Building Boost for Android with error “cannot find -lrt”

编辑tools/build/src/tools/gcc.jam rule setup-threading ( targets * : sources * : properties * ){ local threading = [ feature.get-values threading : $(properties) ] ; if $(threading) = multi { local target = [ feature.get-values target-os : $(properties

如何在多线程leader-follower模式下正确的使用boost::asio。

#include <assert.h> #include <signal.h> #include <unistd.h> #include <iostream> #include <string> #include <deque> #include <set> #include "boost/asio.hpp" #include "boost/thread.hpp" #include