• muduo源码学习base——Atomic(原子操作与原子整数)


    前置知识

    happens-before:

    • 用来描述两个操作的内存可见性 如果操作 X happens-before 操作 Y,那么 X 的结果对于 Y 可见

    六种内存序:

    typedef enum memory_order {
       memory_order_relaxed,   // relaxed不对执行顺序做保证
       memory_order_consume,   // consume暂时不鼓励使用 memory_order_consume
       memory_order_acquire,   // acquire本线程中,所有后续的读操作必须在本条原子操作完成后执行
       memory_order_release,   // release本线程中,所有之前的写操作完成后才能执行本条原子操作
       memory_order_acq_rel,   // acquire/release同时包含memory_order_acquire 和 memory_order_release
       memory_order_seq_cst    // sequentially consistent保证指令的顺序一致执行,不打开编译器优化指令,按照正常的指令序执行
    } memory_order;
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8

    gcc的原子操作__sync_(不推荐):使用这些原子操作gcc编译时要加上选项 -march=cpu-type

    • bool __sync_bool_compare_and_swap (type *ptr, type oldval, type newval, ...)
      比较*ptr与oldval的值,如果相等则将newval更新到*ptr并返回true
    • type __sync_val_compare_and_swap (type *ptr, type oldval, type newval, ...)
      比较*ptr与oldval的值,如果相等则将newval更新到*ptr并返回操作之前*ptr的值
    • type __sync_fetch_and_add (type *ptr, type value, ...)加,返回更新前的值
    • type __sync_fetch_and_sub (type *ptr, type value, ...)
    • type __sync_fetch_and_or (type *ptr, type value, ...)
    • type __sync_fetch_and_and (type *ptr, type value, ...)
    • type __sync_fetch_and_xor (type *ptr, type value, ...)异或
    • type __sync_fetch_and_nand (type *ptr, type value, ...)与非
    • type __sync_add_and_fetch (type *ptr, type value, ...)返回更新后的值
    • type __sync_sub_and_fetch (type *ptr, type value, ...)
    • type __sync_or_and_fetch (type *ptr, type value, ...)
    • type __sync_and_and_fetch (type *ptr, type value, ...)
    • type __sync_xor_and_fetch (type *ptr, type value, ...)
    • type __sync_nand_and_fetch (type *ptr, type value, ...)

    原子操作__atomic:(C++11后推荐,使用内存序)

    • type __atomic_add_fetch(type *ptr, type val, int memorder)
    • type __atomic_sub_fetch(type *ptr, type val, int memorder)
    • type __atomic_and_fetch(type *ptr, type val, int memorder)
    • type __atomic_xor_fetch(type *ptr, type val, int memorder)
    • type __atomic_or_fetch(type *ptr, type val, int memorder)
    • type __atomic_nand_fetch(type *ptr, type val, int memorder)
    • type __atomic_fetch_add(type *ptr, type val, int memorder)
    • type __atomic_fetch_sub(type *ptr, type val, int memorder)
    • type __atomic_fetch_and(type *ptr, type val, int memorder)
    • type __atomic_fetch_xor(type *ptr, type val, int memorder)
    • type __atomic_fetch_or(type *ptr, type val, int memorder)
    • type __atomic_fetch_nand(type *ptr, type val, int memorder)
    • type __atomic_load_n (type *ptr,int memorder);_n表示加不加字节序memorder,
    • void__atomic_store_n (type *ptr, type val,int memorder);
    • type __atomic_exchange_n (type *ptr, type val,int memorder);
    • bool__atomic_compare_exchange_n (type *ptr, type *expected, type desired,bool weak, int success_memorder,int failure_memorder);
    • bool __atomic_test_and_set (void *ptr, int memorder)原子地更改 obj 所指向的 atomic_flag 的状态为设置( true )
    • void __atomic_clear (bool *ptr, int memorder)
    • void __atomic_thread_fence (int memorder)
    • bool __atomic_always_lock_free (size_t size, void *ptr)
    • bool __atomic_is_lock_free (size_t size, void *ptr)

    谷歌规范const常量命名加k:const int kvalue


    关于gcc的编译告警选项:
    在这里插入图片描述

    AtomicIntegerT

    muduo::detail::AtomicIntegerT是noncopyable的派生类,不可拷贝

    class AtomicIntegerT : noncopyable
    
    • 1

    类图:
    在这里插入图片描述

    • volatile T value_: 用于原子操作的value_值,volatile表示直接从内存取,不从寄存器取,与该变量有关的运算,不要进行编译优化,以免出错
    • 所有其他成员函数都是通过get(), getAndAdd(), getAndSet()来实现的

    get()

    T get()
    {
     // in gcc >= 4.7: __atomic_load_n(&value_, __ATOMIC_SEQ_CST)
     return __sync_val_compare_and_swap(&value_, 0, 0);
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5

    注意选项-march=cpu-type

    getAndAdd()

    T getAndAdd(T x)
    {
     // in gcc >= 4.7: __atomic_fetch_add(&value_, x, __ATOMIC_SEQ_CST)
     return __sync_fetch_and_add(&value_, x);
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5

    注意选项-march=cpu-type

    getAndSet()

    T getAndSet(T newValue)
    {
     // in gcc >= 4.7: __atomic_exchange_n(&value_, newValue, __ATOMIC_SEQ_CST)
     return __sync_lock_test_and_set(&value_, newValue);
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5

    注意选项-march=cpu-type

    关于原子操作实现无锁队列(lock-free-queue)

    无锁队列的实现

  • 相关阅读:
    辅助驾驶功能开发-功能规范篇(16)-2-领航辅助系统NAP-功能ODD定义
    springboot
    【Pytest实战】Pytest+Allure+Jenkins自动化测试框架搭建
    JVM架构和内存管理优化
    想了一个月都不知道如何开始做自媒体
    博世XC事业部李胤:自动驾驶降温不意外,但这条路肯定会走下去
    Ansible 企业实战详解
    【SpringSecurity】十一、SpringSecurity集成JWT实现token的方法与校验
    2023年中国功效护肤品市场发展概况分析:行业市场成熟度高[图]
    2023最新性能测试八股文【附答案】,软测人必备!
  • 原文地址:https://blog.csdn.net/qq_41420788/article/details/128160274