c++ 无锁队列的简单实现

简介: c++ 无锁队列的简单实现

无锁队列的基本介绍


一个关于无锁队列的多线程读写代码示例。在这里,我提供一个简单的示例来说明这个问题。


在使用无锁队列时,需要注意以下几点:


使用原子操作来实现对队列的读写操作,以避免多线程同时访问同一数据导致的竞争条件问题。


当队列为空或已满时,需要使用特殊的标记来表示队列的状态。


使用链表来实现的无锁队列


下面是一个使用无锁队列的多线程读写代码示例:

#include <atomic>
#include <thread>
#include <iostream>
template <typename T>
class LockFreeQueue
{
public:
    LockFreeQueue() : m_head(new Node), m_tail(m_head.load()) {}
    ~LockFreeQueue()
    {
        while (Node* const old_head = m_head)
        {
            m_head = old_head->next;
            delete old_head;
        }
    }
    void enqueue(T value)
    {
        Node* const new_node = new Node(value);
        Node* old_tail = m_tail.exchange(new_node);
        old_tail->next = new_node;
    }
    bool dequeue(T& value)
    {
        Node* old_head = m_head;
        Node* new_head;
        do
        {
            if (old_head->next == nullptr)
            {
                return false;
            }
            new_head = old_head->next;
        } while (!m_head.compare_exchange_weak(old_head, new_head));
        value = new_head->value;
        delete old_head;
        return true;
    }
private:
    struct Node
    {
        T value;
        Node* next;
        Node() : next(nullptr) {}
        Node(T value) : value(value), next(nullptr) {}
    };
    std::atomic<Node*> m_head;
    std::atomic<Node*> m_tail;
};
int main()
{
    LockFreeQueue<int> queue;
    std::thread t1([&queue]()
    {
        for (int i = 0; i < 10; ++i)
        {
            queue.enqueue(i);
        }
    });
    std::thread t2([&queue]()
    {
        int value = 0;
        while (value < 9)
        {
            if (queue.dequeue(value))
            {
                std::cout << "Dequeued value: " << value << std::endl;
            }
        }
    });
    t1.join();
    t2.join();
    return 0;
}


这段代码实现了一个无锁队列,其中enqueue()函数用于向队列中添加元素,dequeue()函数用于从队列中取出元素。在这个示例中,我们使用了C++11中的std::atomic来实现原子操作,以确保多线程访问时的线程安全。同时,我们使用了compare_exchange_weak()函数来确保多线程环境下的原子操作。


以环形队列实现无锁队列


#include <atomic>
template <typename T>
class LockFreeQueue {
public:
    LockFreeQueue(size_t capacity = 1024) : m_capacity(capacity) {
        m_data = new T[m_capacity];
        m_head.store(0, std::memory_order_relaxed);
        m_tail.store(0, std::memory_order_relaxed);
    }
    ~LockFreeQueue() {
        delete[] m_data;
        m_data = nullptr;
    }
    bool push(const T& item) {
        size_t tail = m_tail.load(std::memory_order_relaxed);
        size_t head = m_head.load(std::memory_order_acquire);
        size_t count = tail - head;
        if (count >= m_capacity - 1) {
            return false;
        }
        m_data[tail % m_capacity] = item;
        m_tail.store(tail + 1, std::memory_order_release);
        return true;
    }
    bool pop(T& item) {
        size_t head = m_head.load(std::memory_order_relaxed);
        size_t tail = m_tail.load(std::memory_order_acquire);
        size_t count = tail - head;
        if (count == 0) {
            return false;
        }
        item = m_data[head % m_capacity];
        m_head.store(head + 1, std::memory_order_release);
        return true;
    }
private:
    T* m_data;
    size_t m_capacity;
    std::atomic<size_t> m_head;
    std::atomic<size_t> m_tail;
};


这个队列是一个环形队列,使用了两个原子变量 m_headm_tail 分别表示队列头和队列尾,通过利用原子操作保证线程安全,实现了无锁的操作。同时,使用了 memory_order 来保证数据的可见性和原子性。

相关文章
|
24天前
|
设计模式 算法 C++
【C++初阶】12. Stack(栈)和Queue(队列)
【C++初阶】12. Stack(栈)和Queue(队列)
42 3
|
2月前
|
算法 调度 C++
【C/C++ 数据结构 线性表】C/C++中队列的原理与实现:从基础到循环队列
【C/C++ 数据结构 线性表】C/C++中队列的原理与实现:从基础到循环队列
41 0
|
6月前
|
C++
C/C++ DLL 简单实现
C/C++ DLL 简单实现
44 0
|
5月前
|
设计模式 C++ iOS开发
【C++】STL容器适配器入门:【堆】【栈】【队列】(16)
【C++】STL容器适配器入门:【堆】【栈】【队列】(16)
【C++】STL容器适配器入门:【堆】【栈】【队列】(16)
|
7天前
|
设计模式 C语言 C++
【C++进阶(六)】STL大法--栈和队列深度剖析&优先级队列&适配器原理
【C++进阶(六)】STL大法--栈和队列深度剖析&优先级队列&适配器原理
|
2月前
|
算法 数据安全/隐私保护 C++
【C/C++ 编程题 03】用两个栈实现一个队列的功能
【C/C++ 编程题 03】用两个栈实现一个队列的功能
16 0
|
2月前
|
算法 编译器 C语言
【C/C++ 编程题 02】用两个栈实现一个队列的功能
【C/C++ 编程题 02】用两个栈实现一个队列的功能
20 0
|
2月前
|
安全 算法 调度
C++队列探秘:队列容器的使用技巧与实战案例解析
C++队列探秘:队列容器的使用技巧与实战案例解析
128 0
|
4月前
|
Rust
2023年博客之星入围选拔重装开启——今年没有拉票环节啦
2023年博客之星入围选拔重装开启——今年没有拉票环节啦
31 0
2023年博客之星入围选拔重装开启——今年没有拉票环节啦
|
4月前
|
C++ Python Java
Python每日一练(20230422) 杨辉三角、最长回文子串、逆波兰表达式求值
Python每日一练(20230422) 杨辉三角、最长回文子串、逆波兰表达式求值
18 0
Python每日一练(20230422) 杨辉三角、最长回文子串、逆波兰表达式求值