STL的queue多线程下是不安全的,当然网上有很多无锁队列,如boost::lockfree::queue就是很好的东西。这里实现一个互斥锁的线程安全队列,基于STL的QUEUE。
/************************************************************************************************
File Name : thread_safe_queue.hpp
Version : Initial Draft
Author : yang yang
Created : 2017.06.08
Last Modified: 2017.06.08
Description : A thread safe queue,use the queue of STL,and Boost.
************************************************************************************************/
#ifndef THREAD_SAFE_QUEUE_HPP
#define THREAD_SAFE_QUEUE_HPP
#include <queue>
#include <boost/thread.hpp>
#include <boost/lambda/lambda.hpp>
template <typename T> class ThreadSafeQueue
{
private:
std::queue<T> queue;
boost::mutex mutex;
public:
ThreadSafeQueue(){};
ThreadSafeQueue(const ThreadSafeQueue &other)
{

本文介绍如何在多线程环境下确保STL queue的安全性,通过使用Boost库中的互斥锁来实现线程安全的队列,提供了一种替代无锁队列如boost::lockfree::queue的方法。

6750

被折叠的 条评论
为什么被折叠?



