一个无锁队列

在github上,发现一个非阻塞多消费者多生产者无锁队列,基于c++11实现,并且只有一个头文件,而这个头文件只有200多行代码。

alignas、false sharing和true sharing

alignas是c++11的一个关键字,限制内存地址的开始位置。
这里有个要注意的地方,用alignas修饰struct/class时,假设使用malloc分配了内存,在用new在此内存上构造一个对象,会出现不符合alignas要求的情况。

struct alignas(256) A
{
   
  int a_;
};
int main()
{
   
  void* p = malloc(sizeof(A));
  A* pA = new (p) A(); /* 这里的内存开始地址就不一定符合alignas(256)要求 */
  A* pB = new A(); /* 这里的内存开始地址符合alignas(256)要求 */
  return 0;
}

/* 出现这种区别原因是因为new有多个重载版本,其中一个如下 */
void* __CRTDECL operator new(
    size_t           _Size,
    std::align_val_t _Al
    );

false sharing,简单来说就是当两个成员变量在同一个cache line时,其中一个成员变量被修改,会导致cache line无效,另一个成员变量就没法享受cache带来的好处了。所以有时候要避免false sharing。

true sharing,如果一个结构体的sizeof的大小小于或等于cache line大小,那么这个结构体的成员间就存在true sharing。

c++17提供了std::hardware_destructive_interference_size来避免false sharing,std::hardware_constructive_interference_size来检测是否存在true sharing。

memory order

memory_order_acquire——用于一个原子变量的load操作,所有读、写不能reorder到此load之前。并且,如果其他线程对同一个原子变量进行release的store操作,那么这个线程的所有写操作对于load操作所在的线程可见。

memory_order_release——用于一个原子变量的store操作,所有读、写不能reorder到此store之后。并且,此store所在的线程的所有写操作对于对同一个原子变量进行acquire的load操作所在的线程可见。

MPMCQueue

MPMCQueue内部维护一个数组Slots,长度为Capacity,两个下标Head和Tail。Head与Tail只能向前,每次push时,Head加1;每次pop时,Tail加1。故而可以通过Head或者Tail除以数组长度得到现在是多少轮,通过Head或者Tail对数组长度求模得到数组的下标。数组Slots中每个元素都有一个Turn表明现在是多少轮,可读还是可写。还有一个T是用于保存队列的数据。

假设数组满了的情况下,再尝试添加元素,结果是失败返回或者busy wait,不会覆写之前的数据。
在这里插入图片描述

/*
Copyright (c) 2020 Erik Rigtorp <erik@rigtorp.se>
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
 */

#pragma once

#include <atomic>
#include <cassert>
#include <cstddef> // offsetof
#include <limits>
#include <memory>
#include <new> // std::hardware_destructive_interference_size
#include <stdexcept>

#ifndef __cpp_aligned_new
#ifdef _WIN32
#include <malloc.h> // _aligned_malloc
#else
#include <stdlib.h> // posix_memalign
#endif
#endif

namespace rigtorp {
   
namespace mpmc {
   
#ifdef __cpp_lib_hardware_interference_size
static constexpr size_t hardwareInterferenceSize =
    std::hardware_destructive_interference_size;
#else
static constexpr size_t hardwareInterferenceSize = 64;
#endif

#if defined(__cpp_aligned_new)
template <typename T> using AlignedAllocator = std::allocator<T>;
#else
template <typename T> struct AlignedAllocator {
   
  using value_type = T;

  T *allocate(std::size_t n) {
   
    if (n > std::numeric_limits<std::size_t>::max() / sizeof(T)) {
   
      throw std::bad_array_new_length();
    }
#ifdef _WIN32
    auto *p = static_cast<T *>(_aligned_malloc(sizeof(T) * n, alignof(T)));
    if (p == nullptr) {
   
      throw std::bad_alloc()
  • 1
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值