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

本文介绍了Atomic操作的基础概念,如happens-before原则和内存序,以及GCC中的__sync_和__atomic原子操作。重点讨论了AtomicIntegerT类的get(),getAndAdd(),getAndSet()方法及其在无锁队列实现中的应用,提醒读者注意编译选项-march=cpu-type的影响。
摘要由CSDN通过智能技术生成

前置知识

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;

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

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

  • 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);
}

注意选项-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);
}

注意选项-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);
}

注意选项-march=cpu-type

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

无锁队列的实现

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值