C++ 并发专题 - 实现一个线程安全的队列

一:概述

本文利用 C++ 标准库中的多线程、条件变量、互斥锁等工具来实现一个线程安全的队列,并且使用多个线程来向队列中添加和获取数据。

二:实现过程:

cpp 复制代码
#include <iostream>
#include <queue>
#include <mutex>
#include <condition_variable>
#include <thread>
#include <vector>

template <typename T>
class ThreadSafeQueue {
public:
    // 向队列中添加元素
    void push(const T& value) {
        std::lock_guard<std::mutex> lock(mutex_);
        queue_.push(value);
        cond_var_.notify_one();  // 通知一个等待的线程
    }

    // 从队列中取出元素,如果队列为空,阻塞等待
    T pop() {
        std::unique_lock<std::mutex> lock(mutex_);
        cond_var_.wait(lock, [this] { return !queue_.empty(); });  // 等待直到队列非空
        T value = queue_.front();
        queue_.pop();
        return value;
    }

    // 判断队列是否为空
    bool empty() const {
        std::lock_guard<std::mutex> lock(mutex_);
        return queue_.empty();
    }

private:
    mutable std::mutex mutex_;            // 互斥锁,保护队列
    std::queue<T> queue_;                 // 基础队列
    std::condition_variable cond_var_;   // 条件变量,用于队列为空时的等待
};

// 示例:使用线程安全队列
void producer(ThreadSafeQueue<int>& queue, int numItems) {
    for (int i = 0; i < numItems; ++i) {
        queue.push(i);
        std::cout << "Produced: " << i << std::endl;
    }
}

void consumer(ThreadSafeQueue<int>& queue, int numItems) {
    for (int i = 0; i < numItems; ++i) {
        int item = queue.pop();
        std::cout << "Consumed: " << item << std::endl;
    }
}

int main() {
    ThreadSafeQueue<int> queue;

    const int numItems = 10;
    const int numProducers = 2;
    const int numConsumers = 2;

    std::vector<std::thread> threads;

    // 启动生产者线程
    for (int i = 0; i < numProducers; ++i) {
        threads.push_back(std::thread(producer, std::ref(queue), numItems / numProducers));
    }

    // 启动消费者线程
    for (int i = 0; i < numConsumers; ++i) {
        threads.push_back(std::thread(consumer, std::ref(queue), numItems / numConsumers));
    }

    // 等待所有线程完成
    for (auto& thr : threads) {
        thr.join();
    }

    return 0;
}
相关推荐
(Charon)8 分钟前
【C++】网络缓冲区设计(二):Ring Buffer环形缓冲区、head/tail与跨界读写
开发语言·c++
Java后端的Ai之路18 分钟前
LangChain Deep Agents 从入门到企业实战
开发语言·人工智能·python·langchain·deepagents
会飞的拖把22 分钟前
Python文件操作详解:从文件读写到os、shutil模块实战
开发语言·python
RS迷途小书童27 分钟前
Python 解析大疆无人机 SRT 字幕日志
开发语言·python·无人机
点心的游戏开发世界1 小时前
GDScript 入门笔记(十六):文件读写与数据持久化
开发语言·笔记·游戏引擎·godot
会飞的拖把1 小时前
Python序列详解:列表、元组、字符串的通用操作(超详细版)
开发语言·windows·python
reasonsummer1 小时前
【办公类-115-01】20260906育儿知识(家园小报)批量制作(2026年9月-2027年6月)
开发语言·数据库·c#
君顾11 小时前
智慧场馆解决方案小程序系统实战:从架构设计到上线指南
java·开发语言·智慧场馆
zhougl9961 小时前
Dockerfile实战教程
java·开发语言·spring boot
FfHUCisI2 小时前
Golang 网络轮询器 netpoll:把阻塞 IO 变成事件通知
开发语言·网络·golang