这是根据 Maged M. Michael and Michael L. Scott提出的双锁并发队列算法实现的c++版本,主要应用场景为:单生产者/多消费者或多生产者/多消费者或多生产者/单消费者的情况,在实现中,我们用spinlock作为互斥锁;内存分配采用了无锁内存池进行分配。
<pre name="code" class="cpp">//
// aqueue.h
// nebula
//
// Created by yi.cheng on 15/10/8.
// Copyright © 2015 year kanshansoft. All rights reserved.
//
#ifndef aqueue_h
#define aqueue_h
#include <core/mem/lfalloctor.h>
#include <core/Errors.h>
#include <core/guard.h>
#include <utils/Log.h>
/**
Two-Lock Concurrent Queue Algorithm
structure node_t {value: data type, next: pointer to node_t}
structure queue_t {Head: pointer to node_t, Tail: pointer to node_t,
H_lock: lock type, T_lock: lock type}
initialize(Q: pointer to queue_t)
node = new_node() // Allocate a free node
node->next = NULL // Make it the only node in the linked list
Q->Head = Q->Tail = node // Both Head and Tai