c++11多线程编程---线程安全队列
来源:互联网 发布:如何优化前端页面 编辑:程序博客网 时间:2024/06/13 18:14
线程安全队列
c11正式引入了自己的线程类,让c++的多线程编程变的更加优雅。由于不同的编译器对新特性的支持有所差异,这里的代码都是在gcc 4.8版本下编译运行。
涉及到的C++11的知识如下:
- thread 线程库
- 同步和互斥有关量:mutex 和 condition_variable
- 简单的资源管理类:lock_guard,unique_lock,shared_ptr.
- lambda表达式
底层容器为queue,这里将取元素front() 和弹出pop()操作合并为一个接口,为了防止以下事件:在取元素和弹出元素之间这段时间又有其他线程进行front()或者pop(),出现 错误。
#include <iostream>#include <thread>#include <condition_variable>#include <mutex>#include <memory>#include <algorithm>#include <vector>#include <string>#include <queue>using namespace std;template<typename T>class safe_queue{public: mutex m_mut; condition_variable m_cond; queue<T> m_queue; public: safe_queue(){} safe_queue(const safe_queue& rhs) { lock_guard<mutex> lk(rhs.m_mut); m_queue = rhs; } void push(T data) { lock_guard<mutex> lk(m_mut); // 使用效率高的lock_guard m_queue.push(move(data)); // 移动构造函数,防止对象不能拷贝 m_cond.notify_one(); // 通知唤醒阻塞的一个线程 } void wait_and_pop(T &res) // 直到队列不为空 { unique_lock<mutex> lk(m_mut); m_cond.wait(lk,[this]{return !m_queue.empty();}); res = move(m_queue.front()); m_queue.pop(); } bool try_pop(T &res) //立即返回 { lock_guard<mutex> lk(m_mut); if(m_queue.empty()) return false; res = move(m_queue.front()); return true; } /* 下面这种是由返回值返回元素。 */ shared_ptr<T> wait_and_pop() { unique_lock<mutex> lk(m_mut); m_cond.wait(lk,[this]{return !m_queue.empty();}); shared_ptr<T> res(make_shared<T>(move(m_queue.front()))); m_queue.pop(); return res; } shared_ptr<T> try_pop() { lock_guard<mutex> lk(m_mut); if(m_queue.empty()) return NULL; shared_ptr<T> res(make_shared<T>(move(m_queue.front()))); m_queue.pop(); return res; } };
测试:
void func(safe_queue<string> &que){ while(1) // 不断的处理数据 { string a; que.wait_and_pop(a); // 没有元素则阻塞 cout<<"thread_id :"<<this_thread::get_id(); cout<<" input is: "<<a<<endl; }}int main(){ safe_queue<string> que; vector<thread> threads; for(int i = 0;i<5;++i) // 这里创建5个测试线程 { threads.push_back(thread(func,ref(que))); } string a; while(cin>>a) // 数据准备线程,在不断的获取数据。 { que.push(a); } for_each(threads.begin(),threads.end(),mem_fn(&thread::join)); //调用所有线程的join函数,等待任务完成 return 0;}
测试结果如下:
0 0
- c++11多线程编程---线程安全队列
- linux多线程编程(C):信号量实现的线程安全队列
- linux多线程编程(C):互斥量实现的线程安全队列
- C/C++ 线程安全队列
- C++11线程安全队列
- Java多线程编程与线程安全
- 多线程编程 -- 线程安全的链表
- 多线程编程-- 线程安全的栈 stack
- 多线程编程-- 线程安全的queue II
- 多线程编程之线程安全退出
- Java多线程总结之线程安全队列Queue
- Java多线程总结之线程安全队列Queue
- Java多线程总结之线程安全队列Queue
- sqlite4java中的多线程队列使用(线程安全)
- Java多线程总结之线程安全队列Queue
- Java多线程总结之线程安全队列Queue
- Java多线程总结之线程安全队列Queue
- Java多线程总结之线程安全队列Queue
- Django笔记 ”coercing to Unicode: need string or buffer, int found“
- PYTHON属性访问
- 多个github帐号的SSH key切换
- 蓝桥网 算法训练 方格取数
- MAC下用homebrew安装及配置apache、php和mysql
- c++11多线程编程---线程安全队列
- 数据结构之非递归遍历和层次遍历(C语言版)
- Boost.Lockfree
- 8086逻辑运算和移位
- 安卓Andriod使用入门(一)【圆形Menu菜单】
- D-Link DP-LINK302打印服务器WIN7版软件
- 用友T3建立年度帐提示:对象名TAX_SB_NSSB_BJ_ZB无效
- Windows Server 2003修改系列号
- Windows2012微软取消了服务器桌面个性化选项