c++线程安全队列

SafeQueue.h

#pragma once
#include <iostream>
#include <string>
#include <cstring>
 #include <queue>
 #include <unistd.h>
 #include <mutex>
 #include <condition_variable>
#include <pthread.h>


template <typename T>
class SafeQueue {
public:
    using lock_type = std::unique_lock<std::mutex>;
public:
    SafeQueue() = default;
    // 手动添加拷贝构造
    SafeQueue(const SafeQueue& other)
    {
        // std::lock_guard<std::mutex> lock(other.mutex_);
        lock_type lock{mutex_};
        queue_ = other.queue_;
    }
    ~SafeQueue() = default;
    template<typename IT>
    void push(IT &&item) {
        static_assert(std::is_same<T, std::decay_t<IT>>::value, "Item type is not convertible!!!");
        {
        lock_type lock{mutex_};
        queue_.emplace(std::forward<IT>(item));
        }
        cv_.notify_one();
    }
    auto pop() -> T {
        lock_type lock{mutex_};
        cv_.wait(lock, [&]() { return !queue_.empty(); });
        auto front = std::move(queue_.front());
        queue_.pop();
        return front;
    }

    // - `empty()`:检查队列是否为空。
    // 通过创建互斥锁对象,锁定临界区并使用STL `queue::empty()` 成员函数检查元素是否为空来实现。
    bool empty() const {
        lock_type lock{mutex_};
        return queue_.empty();
    }

    // - `size()`:返回队列中元素的数量。
    // 通过创建互斥锁对象,锁定临界区并使用STL `queue::size()` 成员函数返回队列中元素的数量来实现。
    size_t size() const {
        lock_type lock{mutex_};
        return queue_.size();
    }
    
    // - `swap()`:用另一个队列对象交换这个队列对象的内容。
    // 由于交换操作涉及两个不同的队列对象,因此需要创建两个互斥锁对象分别锁定两个队列对象,
    // 然后使用STL `queue::swap()` 成员函数在两个队列对象之间交换内容来实现。
    void swap(SafeQueue<T>& other) {
        lock_type lock1{mutex_, std::defer_lock};
        lock_type lock2{other.mutex_, std::defer_lock};
        std::lock(lock1, lock2);
        std::swap(queue_, other.queue_);
    }

    // - `clear()`:清空队列中的所有元素。
    // 通过创建互斥锁对象,锁定临界区并使用STL `queue::swap()` 成员函数在队列对象中交换备用队列来实现。
    void clear() {
        lock_type lock{mutex_};
        std::queue<T>().swap(queue_);
    }

private:
    std::queue<T> queue_;
    mutable std::mutex mutex_;
    // std::mutex mutex_;
    std::condition_variable cv_;
};

DataBase.h

#pragma once
#include <iostream>
#include <string>
#include <cstring>
#include <sstream>
#include <mutex>
#include "SafeQueue.h"

template <typename T>
class DataBase {
public:
    DataBase() {
        // m_queue.
    }
public:
    static SafeQueue<SafeQueue<T>> m_queue;
    // std::mutex m_mutex;
};

template<typename T>
using SafeQueue2D = SafeQueue<SafeQueue<T>>;

SafeQueue2D<int> testQ;

void init()
{
    for (int i = 0; i < 5; i++)
    {
        SafeQueue<int> row;
        for (int j = 0; j < 5; j++)
        {
            row.push(j + i * 5);
        }
        testQ.push(row);
    }
    
}

void print()
{
    init();
    std::ostringstream ss;
    while (!testQ.empty()) {
        auto row = testQ.pop();
        while (!row.empty()) {
            ss << row.pop() << " ";
        }
        ss << '\n'; // 一行结束
    }
    std::cout << ss.str() << std::endl;
}



Config.h


#pragma once
#include <iostream>

typedef enum tag_enum_ {
    DB_TYPE0 = 0,
    DB_TYPE1,
    DB_TYPE2,
    DB_TYPE3,
    DB_TYPE4,
    DB_TYPE5,
} DataType;

main.c

#include <iostream>
#include <string>
#include <cstring>
#include "DataBase.h"
#include "Config.h"

int main()
{

    std::cout << "hello c++" << std::endl;

    // DataBase<int> db;
    // db.m_queue[0].push(1);
    print();

    return 0;
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值