zoukankan      html  css  js  c++  java
  • C++ std::atomic_flag 实现spinLock

    A spinlock mutex can be implemented in userspace using an atomic_flag.

    一旦标识对象初始化完成,只能做三种操作:销毁、清除或设置并查询其先前的值。

    这些分别对应析构函数、clear()函数以及test_and_set()函数。

    clear和test_and_set操作都可以指定一个内存顺序,

    clear是清除操作(atomically sets flag to false),

    test_and_set是读改写操作(atomically sets the flag to true and obtains its previous value)

    c++20 引入了 test 操作,即读数据操作(atomically returns the value of the flag)

     1 #include <thread>
     2 #include <vector>
     3 #include <iostream>
     4 #include <atomic>
     5 
     6 inline void cpu_relax() {
     7 #if defined(__aarch64__)
     8     asm volatile("yield" ::: "memory");
     9 #else
    10     asm volatile("rep; nop" ::: "memory");
    11 #endif
    12 }
    13 
    14 std::atomic_flag lock = ATOMIC_FLAG_INIT;
    15 
    16 void f(int n)
    17 {
    18     for (int cnt = 0; cnt < 100; ++cnt) {
    19         while (lock.test_and_set(std::memory_order_acquire))  // acquire lock
    20              cpu_relax(); // spin
    21         std::cout << "Output from thread " << n << '
    ';
    22         lock.clear(std::memory_order_release);               // release lock
    23     }
    24 }
    25 
    26 int main()
    27 {
    28     std::vector<std::thread> v;
    29     for (int n = 0; n < 10; ++n) {
    30         v.emplace_back(f, n);
    31     }
    32     for (auto& t : v) {
    33         t.join();
    34     }
    35 }
  • 相关阅读:
    模板---templates
    django框架基础
    文件操作
    C++异常处理基本句法测试
    模板类在包含友元情况下的分离编写
    C++中构造函数作用
    hex文件和bin文件区别
    Keil MDK 和 IAR 两款ARM开发工具区别比较
    ubuntu下安装stm32开发环境
    IAR安装破解教程
  • 原文地址:https://www.cnblogs.com/rayfloyd/p/14306461.html
Copyright © 2011-2022 走看看