C++11 并发指南六( <atomic> 类型详解二 std::atomic )

本文深入解析C++11中std::atomic的基本类型及其成员函数,包括构造函数、赋值操作、基本操作如is_lock_free、store、load等,以及exchange、compare_exchange_weak和compare_exchange_strong等高级操作。

C++11 并发指南六( <atomic> 类型详解二 std::atomic )


C++11 并发指南六(atomic 类型详解一 atomic_flag 介绍)  一文介绍了 C++11 中最简单的原子类型 std::atomic_flag,但是 std::atomic_flag 过于简单,只提供了 test_and_set 和 clear 两个 API,不能满足其他需求(如 store, load, exchange, compare_exchange 等),因此本文将介绍功能更加完善的 std::atomic 类。

std::atomic 基本介绍

std::atomic 是模板类,一个模板类型为 T 的原子对象中封装了一个类型为 T 的值。

template <class T> struct atomic;

原子类型对象的主要特点就是从不同线程访问不会导致数据竞争(data race)。因此从不同线程访问某个原子对象是良性 (well-defined) 行为,而通常对于非原子类型而言,并发访问某个对象(如果不做任何同步操作)会导致未定义 (undifined) 行为发生。

C++11 标准中的基本 std::atomic 模板定义如下:

复制代码
template < class T > struct atomic {
    bool is_lock_free() const volatile;
    bool is_lock_free() const;
    void store(T, memory_order = memory_order_seq_cst) volatile;
    void store(T, memory_order = memory_order_seq_cst);
    T load(memory_order = memory_order_seq_cst) const volatile;
    T load(memory_order = memory_order_seq_cst) const;
    operator  T() const volatile;
    operator  T() const;
    T exchange(T, memory_order = memory_order_seq_cst) volatile;
    T exchange(T, memory_order = memory_order_seq_cst);
    bool compare_exchange_weak(T &, T, memory_order, memory_order) volatile;
    bool compare_exchange_weak(T &, T, memory_order, memory_order);
    bool compare_exchange_strong(T &, T, memory_order, memory_order) volatile;
    bool compare_exchange_strong(T &, T, memory_order, memory_order);
    bool compare_exchange_weak(T &, T, memory_order = memory_order_seq_cst) volatile;
    bool compare_exchange_weak(T &, T, memory_order = memory_order_seq_cst);
    bool compare_exchange_strong(T &, T, memory_order = memory_order_seq_cst) volatile;
    bool compare_exchange_strong(T &, T, memory_order = memory_order_seq_cst);
    atomic() = default;
    constexpr atomic(T);
    atomic(const atomic &) = delete;
    atomic & operator=(const atomic &) = delete;
    atomic & operator=(const atomic &) volatile = delete;
    T operator=(T) volatile;
    T operator=(T);
};
复制代码

另外,C++11 标准库 std::atomic 提供了针对整形(integral)和指针类型的特化实现,分别定义如下:

针对整形(integal)的特化,其中 integal 代表了如下类型char, signed char, unsigned char, short, unsigned short, int, unsigned int, long, unsigned long, long long, unsigned long long, char16_t, char32_t, wchar_t:

template  <> struct  atomic<integral> {
     bool  is_lock_free() const  volatile ;
     bool  is_lock_free() const ;
 
     void  store(integral, memory_order = memory_order_seq_cst) volatile ;
     void  store(integral, memory_order = memory_order_seq_cst);
 
     integral load(memory_order = memory_order_seq_cst) const  volatile ;
     integral load(memory_order = memory_order_seq_cst) const ;
 
     operator integral() const  volatile ;
     operator integral() const ;
 
     integral exchange(integral, memory_order = memory_order_seq_cst) volatile ;
     integral exchange(integral, memory_order = memory_order_seq_cst);
 
     bool  compare_exchange_weak(integral&, integral, memory_order, memory_order) volatile ;
     bool  compare_exchange_weak(integral&, integral, memory_order, memory_order);
 
     bool  compare_exchange_strong(integral&, integral, memory_order, memory_order) volatile ;
     bool  compare_exchange_strong(integral&, integral, memory_order, memory_order);
 
     bool  compare_exchange_weak(integral&, integral, memory_order = memory_order_seq_cst) volatile ;
     bool  compare_exchange_weak(integral&, integral, memory_order = memory_order_seq_cst);
 
     bool  compare_exchange_strong(integral&, integral, memory_order = memory_order_seq_cst) volatile ;
     bool  compare_exchange_strong(integral&, integral, memory_order = memory_order_seq_cst);
 
     integral fetch_add(integral, memory_order = memory_order_seq_cst) volatile ;
     integral fetch_add(integral, memory_order = memory_order_seq_cst);
 
     integral fetch_sub(integral, memory_order = memory_order_seq_cst) volatile ;
     integral fetch_sub(integral, memory_order = memory_order_seq_cst);
 
     integral fetch_and(integral, memory_order = memory_order_seq_cst) volatile ;
     integral fetch_and(integral, memory_order = memory_order_seq_cst);
 
     integral fetch_or(integral, memory_order = memory_order_seq_cst) volatile ;
     integral fetch_or(integral, memory_order = memory_order_seq_cst);
 
     integral fetch_xor(integral, memory_order = memory_order_seq_cst) volatile ;
     integral fetch_xor(integral, memory_order = memory_order_seq_cst);
     
     atomic() = default ;
     constexpr  atomic(integral);
     atomic( const  atomic&) = delete ;
 
     atomic& operator=( const  atomic&) = delete ;
     atomic& operator=( const  atomic&) volatile  = delete ;
     
     integral operator=(integral) volatile ;
     integral operator=(integral);
     
     integral operator++( int ) volatile ;
     integral operator++( int );
     integral operator--( int ) volatile ;
     integral operator--( int );
     integral operator++() volatile ;
     integral operator++();
     integral operator--() volatile ;
     integral operator--();
     integral operator+=(integral) volatile ;
     integral operator+=(integral);
     integral operator-=(integral) volatile ;
     integral operator-=(integral);
     integral operator&=(integral) volatile ;
     integral operator&=(integral);
     integral operator|=(integral) volatile ;
     integral operator|=(integral);
     integral operator^=(integral) volatile ;
     integral operator^=(integral);
};

针对指针的特化:

template  < class  T> struct  atomic<T*> {
     bool  is_lock_free() const  volatile ;
     bool  is_lock_free() const ;
 
     void  store(T*, memory_order = memory_order_seq_cst) volatile ;
     void  store(T*, memory_order = memory_order_seq_cst);
 
     T* load(memory_order = memory_order_seq_cst) const  volatile ;
     T* load(memory_order = memory_order_seq_cst) const ;
 
     operator T*() const  volatile ;
     operator T*() const ;
 
     T* exchange(T*, memory_order = memory_order_seq_cst) volatile ;
     T* exchange(T*, memory_order = memory_order_seq_cst);
 
     bool  compare_exchange_weak(T*&, T*, memory_order, memory_order) volatile ;
     bool  compare_exchange_weak(T*&, T*, memory_order, memory_order);
 
     bool  compare_exchange_strong(T*&, T*, memory_order, memory_order) volatile ;
     bool  compare_exchange_strong(T*&, T*, memory_order, memory_order);
 
     bool  compare_exchange_weak(T*&, T*, memory_order = memory_order_seq_cst) volatile ;
     bool  compare_exchange_weak(T*&, T*, memory_order = memory_order_seq_cst);
 
     bool  compare_exchange_strong(T*&, T*, memory_order = memory_order_seq_cst) volatile ;
     bool  compare_exchange_strong(T*&, T*, memory_order = memory_order_seq_cst);
 
     T* fetch_add( ptrdiff_t , memory_order = memory_order_seq_cst) volatile ;
     T* fetch_add( ptrdiff_t , memory_order = memory_order_seq_cst);
 
     T* fetch_sub( ptrdiff_t , memory_order = memory_order_seq_cst) volatile ;
     T* fetch_sub( ptrdiff_t , memory_order = memory_order_seq_cst);
 
     atomic() = default ;
     constexpr  atomic(T*);
     atomic( const  atomic&) = delete ;
 
     atomic& operator=( const  atomic&) = delete ;
     atomic& operator=( const  atomic&) volatile  = delete ;
 
     T* operator=(T*) volatile ;
     T* operator=(T*);
     T* operator++( int ) volatile ;
     T* operator++( int );
     T* operator--( int ) volatile ;
     T* operator--( int );
     T* operator++() volatile ;
     T* operator++();
     T* operator--() volatile ;
     T* operator--();
     T* operator+=( ptrdiff_t ) volatile ;
     T* operator+=( ptrdiff_t );
     T* operator-=( ptrdiff_t ) volatile ;
     T* operator-=( ptrdiff_t );
};

std::atomic 成员函数

 好了,对 std::atomic 有了一个最基本认识之后我们来看 std::atomic 的成员函数吧。

std::atomic 构造函数

std::atomic 的构造函数如下:

default (1)
          atomic() noexcept = default;
initialization (2)
constexpr atomic (T val) noexcept;
copy [deleted] (3)
          atomic (const atomic&) = delete;
  1. 默认构造函数,由默认构造函数创建的 std::atomic 对象处于未初始化(uninitialized)状态,对处于未初始化(uninitialized)状态 std::atomic对象可以由 atomic_init 函数进行初始化。
  2. 初始化构造函数,由类型 T初始化一个 std::atomic对象。
  3. 拷贝构造函数被禁用。

请看下例:

#include <iostream>       // std::cout
#include <atomic>         // std::atomic, std::atomic_flag, ATOMIC_FLAG_INIT
#include <thread>         // std::thread, std::this_thread::yield
#include <vector>         // std::vector
 
// 由 false 初始化一个 std::atomic<bool> 类型的原子变量
std::atomic< bool > ready( false );
std::atomic_flag winner = ATOMIC_FLAG_INIT;
 
void  do_count1m( int  id)
{
     while  (!ready) { std::this_thread::yield(); } // 等待 ready 变为 true.
 
     for  ( volatile  int  i=0; i<1000000; ++i) {} // 计数
 
     if  (!winner.test_and_set()) {
       std::cout << "thread #"  << id << " won!\n" ;
     }
}
 
int  main ()
{
     std::vector<std:: thread > threads;
     std::cout << "spawning 10 threads that count to 1 million...\n" ;
     for  ( int  i=1; i<=10; ++i) threads.push_back(std:: thread (count1m,i));
     ready = true ;
 
     for  ( auto & th : threads) th.join();
     return  0;
}
std::atomic::operator=() 函数

std::atomic 的赋值操作函数定义如下:

set value (1)
T operator= (T val) noexcept;
T operator= (T val) volatile noexcept;
copy [deleted] (2)
atomic& operator= (const atomic&) = delete;
atomic& operator= (const atomic&) volatile = delete;

可以看出,普通的赋值拷贝操作已经被禁用。但是一个类型为 T 的变量可以赋值给相应的原子类型变量(相当与隐式转换),该操作是原子的,内存序(Memory Order) 默认为顺序一致性(std::memory_order_seq_cst),如果需要指定其他的内存序,需使用 std::atomic::store()。

#include <iostream>             // std::cout
#include <atomic>               // std::atomic
#include <thread>               // std::thread, std::this_thread::yield
 
std::atomic < int > foo = 0;
 
void  set_foo( int  x)
{
     foo = x; // 调用 std::atomic::operator=().
}
 
void  print_foo()
{
     while  (foo == 0) { // wait while foo == 0
         std::this_thread::yield();
     }
     std::cout << "foo: "  << foo << '\n' ;
}
 
int  main()
{
     std:: thread  first(print_foo);
     std:: thread  second(set_foo, 10);
     first.join();
     second.join();
     return  0;
}
基本 std::atomic 类型操作

本节主要介绍基本 std::atomic 类型所具备的操作(即成员函数)。我们知道 std::atomic 是模板类,一个模板类型为 T 的原子对象中封装了一个类型为 T 的值。本文<std::atomic 基本介绍>一节中也提到了 std::atomic 类模板除了基本类型以外,还针对整形和指针类型做了特化。 特化的 std::atomic 类型支持更多的操作,如 fetch_add, fetch_sub, fetch_and 等。本小节介绍基本 std::atomic 类型所具备的操作:

bool  is_lock_free() const  volatile  noexcept ;
bool  is_lock_free() const  noexcept ;
void  store (T val, memory_order sync = memory_order_seq_cst) volatile  noexcept ;
void  store (T val, memory_order sync = memory_order_seq_cst) noexcept ;
Memory Order 值 Memory Order 类型
memory_order_relaxed Relaxed
memory_order_release Release
memory_order_seq_cst Sequentially consistent
#include <iostream>       // std::cout
#include <atomic>         // std::atomic, std::memory_order_relaxed
#include <thread>         // std::thread
 
std::atomic< int > foo(0); // 全局的原子对象 foo
 
void  set_foo( int  x)
{
     foo.store(x, std::memory_order_relaxed); // 设置(store) 原子对象 foo 的值
}
 
void  print_foo()
{
     int  x;
     do  {
         x = foo.load(std::memory_order_relaxed); // 读取(load) 原子对象 foo 的值
     } while  (x == 0);
     std::cout << "foo: "  << x << '\n' ;
}
 
int  main ()
{
     std:: thread  first(print_foo); // 线程 first 打印 foo 的值
     std:: thread  second(set_foo, 10); // 线程 second 设置 foo 的值
     first.join();
     second.join();
     return  0;
}
T load (memory_order sync = memory_order_seq_cst) const  volatile  noexcept ;
T load (memory_order sync = memory_order_seq_cst) const  noexcept ;
Memory Order 值 Memory Order 类型
memory_order_relaxed Relaxed
memory_order_consume Consume
memory_order_acquire Acquire
memory_order_seq_cst Sequentially consistent
#include <iostream>       // std::cout
#include <atomic>         // std::atomic, std::memory_order_relaxed
#include <thread>         // std::thread
 
std::atomic< int > foo(0); // 全局的原子对象 foo
 
void  set_foo( int  x)
{
     foo.store(x, std::memory_order_relaxed); // 设置(store) 原子对象 foo 的值
}
 
void  print_foo()
{
     int  x;
     do  {
         x = foo.load(std::memory_order_relaxed); // 读取(load) 原子对象 foo 的值
     } while  (x == 0);
     std::cout << "foo: "  << x << '\n' ;
}
 
int  main ()
{
     std:: thread  first(print_foo); // 线程 first 打印 foo 的值
     std:: thread  second(set_foo, 10); // 线程 second 设置 foo 的值
     first.join();
     second.join();
     return  0;
}
operator T() const  volatile  noexcept ;
operator T() const  noexcept ;
#include <iostream>       // std::cout
#include <atomic>         // std::atomic
#include <thread>         // std::thread, std::this_thread::yield
 
std::atomic< int > foo = 0;
std::atomic< int > bar = 0;
 
void  set_foo( int  x)
{
     foo = x;
}
 
void  copy_foo_to_bar()
{
 
     // 如果 foo == 0,则该线程 yield,
     // 在 foo == 0 时, 实际也是隐含了类型转换操作,
     // 因此也包含了 operator T() const 的调用.
     while  (foo == 0) std::this_thread::yield();
 
     // 实际调用了 operator T() const, 将foo 强制转换成 int 类型,
     // 然后调用 operator=().
     bar = static_cast < int >(foo);
}
 
void  print_bar()
{
     // 如果 bar == 0,则该线程 yield,
     // 在 bar == 0 时, 实际也是隐含了类型转换操作,
     // 因此也包含了 operator T() const 的调用.
     while  (bar == 0) std::this_thread::yield();
     std::cout << "bar: "  << bar << '\n' ;
}
 
int  main ()
{
     std:: thread  first(print_bar);
     std:: thread  second(set_foo, 10);
     std:: thread  third(copy_foo_to_bar);
 
     first.join();
     second.join();
     third.join();
     return  0;
}

 

T exchange (T val, memory_order sync = memory_order_seq_cst) volatile noexcept;
T exchange (T val, memory_order sync = memory_order_seq_cst) noexcept;
Memory Order 值 Memory Order 类型
memory_order_relaxed Relaxed
memory_order_consume Consume
memory_order_acquire Acquire
memory_order_release Release
memory_order_acq_rel Acquire/Release
memory_order_seq_cst Sequentially consistent

请看下面例子,各个线程计数至 1M,首先完成计数任务的线程打印自己的 ID,

#include <iostream>       // std::cout
#include <atomic>         // std::atomic
#include <thread>         // std::thread
#include <vector>         // std::vector
 
std::atomic< bool > ready( false );
std::atomic< bool > winner( false );
 
void  count1m ( int  id)
{
     while  (!ready) {}                  // wait for the ready signal
     for  ( int  i = 0; i < 1000000; ++i) {}   // go!, count to 1 million
     if  (!winner.exchange( true )) { std::cout << "thread #"  << id << " won!\n" ; }
};
 
int  main ()
{
     std::vector<std:: thread > threads;
     std::cout << "spawning 10 threads that count to 1 million...\n" ;
     for  ( int  i = 1; i <= 10; ++i) threads.push_back(std:: thread (count1m,i));
     ready = true ;
     for  ( auto & th : threads) th.join();
 
     return  0;
}
(1)
bool compare_exchange_weak (T& expected, T val,
           memory_order sync = memory_order_seq_cst) volatile noexcept;
bool compare_exchange_weak (T& expected, T val,
           memory_order sync = memory_order_seq_cst) noexcept;
(2)
bool compare_exchange_weak (T& expected, T val,
           memory_order success, memory_order failure) volatile noexcept;
bool compare_exchange_weak (T& expected, T val,
           memory_order success, memory_order failure) noexcept;
  • 相等,则用 val 替换原子对象的旧值。
  • 不相等,则用原子对象的旧值替换 expected ,因此调用该函数之后,如果被该原子对象封装的值与参数 expected 所指定的值不相等,expected 中的内容就是原子对象的旧值。
Memory Order 值 Memory Order 类型
memory_order_relaxed Relaxed
memory_order_consume Consume
memory_order_acquire Acquire
memory_order_release Release
memory_order_acq_rel Acquire/Release
memory_order_seq_cst Sequentially consistent
#include <iostream>       // std::cout
#include <atomic>         // std::atomic
#include <thread>         // std::thread
#include <vector>         // std::vector
 
// a simple global linked list:
struct  Node { int  value; Node* next; };
std::atomic<Node*> list_head( nullptr );
 
void  append( int  val)
{
     // append an element to the list
     Node* newNode = new  Node{val, list_head};
 
     // next is the same as: list_head = newNode, but in a thread-safe way:
     while  (!list_head.compare_exchange_weak(newNode->next,newNode)) {}
     // (with newNode->next updated accordingly if some other thread just appended another node)
}
 
int  main ()
{
     // spawn 10 threads to fill the linked list:
     std::vector<std:: thread > threads;
     for  ( int  i = 0; i < 10; ++i) threads.push_back(std:: thread (append, i));
     for  ( auto & th : threads) th.join();
 
     // print contents:
     for  (Node* it = list_head; it!= nullptr ; it=it->next)
         std::cout << ' '  << it->value;
 
     std::cout << '\n' ;
 
     // cleanup:
     Node* it; while  (it=list_head) {list_head=it->next; delete  it;}
 
     return  0;
}
9 8 7 6 5 4 3 2 1 0

 

(1)
bool compare_exchange_strong (T& expected, T val,
           memory_order sync = memory_order_seq_cst) volatile noexcept;
bool compare_exchange_strong (T& expected, T val,
           memory_order sync = memory_order_seq_cst) noexcept;
(2)
bool compare_exchange_strong (T& expected, T val,
           memory_order success, memory_order failure) volatile noexcept;
bool compare_exchange_strong (T& expected, T val,
           memory_order success, memory_order failure) noexcept;
  • 相等,则用 val 替换原子对象的旧值。
  • 不相等,则用原子对象的旧值替换 expected ,因此调用该函数之后,如果被该原子对象封装的值与参数 expected 所指定的值不相等,expected 中的内容就是原子对象的旧值。
Memory Order 值 Memory Order 类型
memory_order_relaxed Relaxed
memory_order_consume Consume
memory_order_acquire Acquire
memory_order_release Release
memory_order_acq_rel Acquire/Release
memory_order_seq_cst Sequentially consistent
#include <iostream>       // std::cout
#include <atomic>         // std::atomic
#include <thread>         // std::thread
#include <vector>         // std::vector
 
// a simple global linked list:
struct  Node { int  value; Node* next; };
std::atomic<Node*> list_head( nullptr );
 
void  append( int  val)
{
     // append an element to the list
     Node* newNode = new  Node{val, list_head};
 
     // next is the same as: list_head = newNode, but in a thread-safe way:
 
     while  (!(list_head.compare_exchange_strong(newNode->next, newNode)));
     // (with newNode->next updated accordingly if some other thread just appended another node)
}
 
int  main ()
{
     // spawn 10 threads to fill the linked list:
     std::vector<std:: thread > threads;
     for  ( int  i = 0; i < 10; ++i) threads.push_back(std:: thread (append, i));
     for  ( auto & th : threads) th.join();
 
     // print contents:
     for  (Node* it = list_head; it!= nullptr ; it=it->next)
         std::cout << ' '  << it->value;
 
     std::cout << '\n' ;
 
     // cleanup:
     Node* it; while  (it=list_head) {list_head=it->next; delete  it;}
 
     return  0;
}


好了,本文花了大量的篇幅介绍 std::atomic 基本类型,下一篇博客我会给大家介绍 C++11 的标准库中std::atomic 针对整形(integral)和指针类型的特化版本做了哪些改进。

<think>我们讨论的是C++中的`std::atomic<bool>`类型,它是一个模板特化,用于布尔类型的原子操作。原子操作意味着这些操作在执行过程中不会被其他线程打断,因此是线程安全的。 对于`std::atomic<bool>`,常用的读写操作包括: 1. **存储(写)操作**: - `store`成员函数:将原子对象设置为指定的值。 - 也可以使用赋值运算符(`=`)进行写操作,但这实际上是调用了`store`,并且使用默认的内存序(顺序一致性)。 2. **加载(读)操作**: - `load`成员函数:返回原子对象的当前值。 - 也可以使用隐式转换(比如将原子对象当作布尔值使用)或者直接使用`operator bool`,但这实际上调用了`load`,并且使用默认的内存序。 3. **其他操作**: - `exchange`:交换值,并返回之前的值。 - `compare_exchange_weak`和`compare_exchange_strong`:比较并交换,用于实现无锁编程。 这些函数都可以指定内存顺序(memory order),以控制操作的同步和顺序约束。 ### 详细说明: #### 1. 存储操作(写) ```cpp void store(bool desired, std::memory_order order = std::memory_order_seq_cst) noexcept; ``` - 将原子对象设置为`desired`。 - 默认内存序为`std::memory_order_seq_cst`(顺序一致性),也可以指定其他内存序,如`std::memory_order_release`。 示例: ```cpp std::atomic<bool> flag(false); flag.store(true); // 使用默认内存序 flag.store(false, std::memory_order_release); // 指定释放内存序 ``` #### 2. 加载操作(读) ```cpp bool load(std::memory_order order = std::memory_order_seq_cst) const noexcept; ``` - 返回原子对象的当前值。 - 默认内存序为`std::memory_order_seq_cst`,也可以指定其他内存序,如`std::memory_order_acquire`。 示例: ```cpp std::atomic<bool> flag; bool value = flag.load(); // 使用默认内存序 bool value2 = flag.load(std::memory_order_acquire); // 指定获取内存序 ``` #### 3. 赋值运算符和类型转换 - 赋值运算符(`operator=`)用于写操作,相当于使用顺序一致性的`store`。 - 类型转换运算符(`operator bool`)用于读操作,相当于使用顺序一致性的`load`。 示例: ```cpp std::atomic<bool> flag; flag = true; // 写操作,等同于 flag.store(true); if (flag) { // 读操作,等同于 if (flag.load()) // ... } ``` #### 4. 交换操作 ```cpp bool exchange(bool desired, std::memory_order order = std::memory_order_seq_cst) noexcept; ``` - 将原子对象设置为`desired`,并返回之前的值。 示例: ```cpp std::atomic<bool> flag(false); bool old = flag.exchange(true); // 将flag设置为true,并返回原来的值false ``` #### 5. 比较并交换操作 ```cpp bool compare_exchange_weak(bool& expected, bool desired, std::memory_order order); ``` - 比较原子对象的值与`expected`: - 如果相等,则用`desired`替换原子对象的值(存储操作),并返回`true`。 - 如果不相等,则将原子对象的当前值写入`expected`(加载操作),并返回`false`。 - 注意:`weak`版本可能会虚假失败(即使比较相等也返回false),但通常比`strong`版本性能更好,特别是在循环中使用。 示例(将flag从false设置为true): ```cpp std::atomic<bool> flag(false); bool expected = false; while (!flag.compare_exchange_weak(expected, true) && expected == false) { // 如果expected被更新为true,说明另一个线程已经修改了,那么我们可以退出循环 // 否则,我们重置expected为false继续尝试 expected = false; } ``` ### 内存序说明 内存序参数用于指定操作的内存顺序约束,常用的有: - `std::memory_order_relaxed`:无同步或顺序约束,只保证原子性。 - `std::memory_order_acquire`:用于加载操作,确保该加载操作之后的所有读写操作不会被重排序到该加载之前。 - `std::memory_order_release`:用于存储操作,确保该存储操作之前的所有读写操作不会被重排序到该存储之后。 - `std::memory_order_seq_cst`:顺序一致性,最严格的内存顺序,也是默认值。 ### 示例代码 以下是一个简单的示例,展示如何使用`std::atomic<bool>`: ```cpp #include <atomic> #include <iostream> #include <thread> std::atomic<bool> ready(false); void worker() { // 等待主线程设置ready为true while (!ready.load(std::memory_order_acquire)) { std::this_thread::yield(); } std::cout << "Worker starts\n"; } int main() { std::thread t(worker); std::cout << "Main thread sets ready\n"; ready.store(true, std::memory_order_release); t.join(); return 0; } ``` ### 注意事项 - 原子操作虽然保证了单个操作的原子性,但多个原子操作组合在一起并不一定是原子的。如果需要多个操作作为一个原子单元,仍然需要使用互斥锁。 - 对于简单的标志位,`std::atomic<bool>`是高效且线程安全的。 §§相关问题§§ 1. 如何选择`std::atomic`的内存序? 2. 多线程环境下原子操作可能引发哪些性能问题? 3. `std::atomic`与`volatile`关键字在并发编程中的区别是什么? 4. 如何用原子变量实现无锁队列? </think> ### `std::atomic<bool>` 的读写函数详解 `std::atomic<bool>` 提供线程安全的原子操作接口,以下是核心读写函数及其用法: #### 1. **存储操作(写操作)** ```cpp void store(bool desired, std::memory_order order = std::memory_order_seq_cst) noexcept; ``` - **功能**:原子地将值 `desired` 写入原子布尔变量 - **参数**: - `desired`:要写入的值(`true`/`false`) - `order`:内存顺序(默认为顺序一致性) - **用法**: ```cpp std::atomic<bool> flag(false); flag.store(true); // 写入true flag.store(false, std::memory_order_release); // 指定内存序 ``` #### 2. **加载操作(读操作)** ```cpp bool load(std::memory_order order = std::memory_order_seq_cst) const noexcept; ``` - **功能**:原子读取当前值 - **用法**: ```cpp if (flag.load()) { // 读取值 // 处理true的情况 } bool current = flag.load(std::memory_order_acquire); // 指定内存序 ``` #### 3. **隐式转换(简化读操作)** ```cpp operator bool() const noexcept; // 隐式转换函数 ``` - **功能**:允许直接作为布尔值使用,相当于调用 `load()` - **用法**: ```cpp if (flag) { // 等价于flag.load() // 执行操作 } ``` #### 4. **赋值运算符(简化写操作)** ```cpp bool operator=(bool desired) noexcept; ``` - **功能**:通过赋值语法原子写入值 - **用法**: ```cpp flag = true; // 等价于flag.store(true) ``` #### 5. **交换操作** ```cpp bool exchange(bool desired, std::memory_order order = std::memory_order_seq_cst) noexcept; ``` - **功能**:原子交换值并返回旧值 - **用法**: ```cpp bool old = flag.exchange(true); // 设置新值并返回旧值 ``` ### 内存顺序参数说明 | 内存序 | 说明 | 适用场景 | |--------|------|---------| | `memory_order_seq_cst` | 严格顺序一致性(默认) | 需要强同步保证 | | `memory_order_relaxed` | 无顺序约束 | 仅需原子性,不要求同步 | | `memory_order_acquire` | 获取操作 | 读操作后需防止乱序 | | `memory_order_release` | 释放操作 | 写操作前需防止乱序 | | `memory_order_acq_rel` | 获取+释放 | Read-Modify-Write操作 | ### 使用示例 ```cpp #include <atomic> #include <iostream> std::atomic<bool> ready(false); // 全局原子标志 // 线程1:设置标志 void setter() { ready.store(true, std::memory_order_release); // 释放语义 } // 线程2:检查标志 void checker() { while (!ready.load(std::memory_order_acquire)) { // 获取语义 // 忙等待 } std::cout << "Flag is set\n"; } int main() { std::thread t1(setter); std::thread t2(checker); t1.join(); t2.join(); } ``` > **关键点**: > 1. `store`和`load`的配对使用确保线程间同步[^1] > 2. 释放-获取内存序防止指令重排 > 3. 原子操作避免数据竞争 ### 性能特点 1. **无锁实现**:现代硬件通常支持原子布尔操作的无锁实现 2. **轻量高效**:比互斥锁性能高10-100倍[^2] 3. **适用场景**:标志位、状态切换等简单操作
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值