基于boost库的线程安全queue
来源:互联网 发布:手机网站自助建站源码 编辑:程序博客网 时间:2024/06/04 18:55
STL的queue多线程下是不安全的,当然网上有很多无锁队列,如boost::lockfree::queue就是很好的东西。这里实现一个互斥锁的线程安全队列,基于STL的QUEUE。
/************************************************************************************************File Name:thread_safe_queue.hppVersion: Initial DraftAuthor:yang yangCreated:2017.06.08Last Modified:2017.06.08Description: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) { boost::mutex::scoped_lock lock(other.mutex); queue = other.queue; } ~ThreadSafeQueue(){}; void Push(T &new_value) { boost::mutex::scoped_lock lock(mutex); queue.push(new_value); condition.notify_one(); } void Pop(T &value) { boost::mutex::scoped_lock lock(mutex); value = queue.front(); queue.pop(); } bool TryPop(T &value) { boost::mutex::scoped_lock lock(mutex); if( queue.empty()) { return false; } else { value = queue.front(); queue.pop(); return true; } } bool Empty() { boost::mutex::scoped_lock lock(mutex); return queue.empty(); }};#endif
阅读全文
0 0
- 基于boost库的线程安全queue
- 线程安全的queue
- boost::shared_ptr的线程安全
- C++11:基于std::queue和std::mutex构建一个线程安全的队列
- 多线程变量--- 线程安全的queue
- 多线程编程-- 线程安全的queue II
- 基于Boost的数据处理器及线程安全队列、跨平台的信号量和互斥锁
- 基于Boost的数据处理器及线程安全队列、跨平台的信号量和互斥锁
- python 8-2 如何线程间通信,使用标准库中Queue.queue,它是一个线程安全的队列
- Java线程安全队列Queue
- [Python标准库]Queue——线程安全的 FIFO 实现
- C++并发实战12:线程安全的queue
- C++并发实战17:线程安全的stack和queue
- C++并发实战12:线程安全的queue
- 模板类高效线程安全的实现Queue
- boost::signal2 信号/插槽(线程安全的观察者模式)
- 基于Spring+Ibatis的安全线程实现
- 基于Spring+Ibatis的安全线程实现
- HDU 2002
- GET请求写法
- java多线程(2)---基于ExecutorService的socket通信线程池
- POST请求
- mysql 忘记root密码怎么办
- 基于boost库的线程安全queue
- js获取当前时间上一个月前的时间
- js获取当前日期
- 多行文本溢出显示省略号(…)全攻略
- 在nginx在windows上搭建流媒体服务器
- 生成GUID的一个很不错的方法
- java知识结构图
- jQuery TagsInput
- Js保留两位小数