使用细粒度锁的c++线程安全队列
来源:互联网 发布:iphone8爆炸知乎 编辑:程序博客网 时间:2024/06/05 07:31
</pre><p>使用细粒度锁的c++线程安全队列实现:</p><p></p><pre name="code" class="cpp">#include<iostream>#include<thread>#include<mutex>#include<memory>template<typename T>class threadsafe_queue{private:struct node{std::shared_ptr<T> data;std::unique_ptr<node> next;};std::unique_ptr<node> head;node* tail;std::mutex head_mutex;std::mutex tail_mutex;node* get_tail(){std::lock_guard<std::mutex> tail_lock(tail_mutex);return tail;}std::unique_ptr<node> pop_head(){std:; lock_guard<std::mutex>head_lock(head_mutex)if (head.get() == get_tail())return nullptr;std::unique_ptr<node>const old_head = std::move(head);head = std::move(old_head->next);return old_head;}public:threadsafe_queue(){}threadsafe_queue(const threadsafe_queue& other) = delete;threadsafe_queue& operator=(const threadsafe_queue &other) = delete;void push(T new_value){std::shared_ptr<T> new_data(std::make_shared<T>(std::move(new_value)));std::unique_ptr<node>p(new node);node* const new_tail = p.get();std::lock_guard < std::mutex。 tail_lock(tail_mutex);tail->data = new_data;tail->next = std::move(p);tail = new_tail;}};
0 0
- 使用细粒度锁的c++线程安全队列
- c++11细粒度的线程安全
- 线程安全的队列
- 线程安全的队列
- 队列的线程安全
- C/C++ 线程安全队列
- linux多线程编程(C):信号量实现的线程安全队列
- linux多线程编程(C):互斥量实现的线程安全队列
- 线程安全的队列写法
- c++线程安全的队列
- 自定义线程安全的队列
- c++ 11 多线线程系列--------使用锁和条件变量的线程安全队列
- 线程安全的可持久队列
- 【例子】线程安全的共享缓冲队列
- 线程安全的循环队列缓冲区
- C++ 实现线程安全的任务队列
- c++11baohan线程安全的队列
- delphi非常简单的线程安全队列
- UI常用组件的监听的方法及控件监听的四种方法
- c#文本框数字校验,不能输入重复值判断
- 使用VisualSVN Server搭建SVN服务器
- ListView异步加载图片,完美实现图文混排
- SSL安全证书
- 使用细粒度锁的c++线程安全队列
- Java中使用Jedis操作Redis
- 安卓 imageview资源图片ID获取方法
- 蓝牙之十八- bluetooth pair
- java注释
- Workbench使用教程
- AFNetworking 3.0+ 启用完整、严格的https证书较验参考代码
- openwrt编译一些应用的过程
- 三大框架SSH成长路程(十九)