boost 线程安全队列
- // QueueImplementation.cpp : Defines the entry point for the console application.
- //
- #include "stdafx.h"
- #include <windows.h>
- #include <iostream>
- #include <queue>
- #include <string>
- #include <process.h>
- using namespace std;
- struct DataBlock
- {
- string m_szText; //sample data
- };
- class CDataQueue
- {
- private:
- queue<DataBlock> m_oQueue; //contains the actual data
- CRITICAL_SECTION m_csData; //to synchroize access to m_csData among multiple threads
- HANDLE m_hEvent; //for signalling presence of absence of data
- public:
- //create a manual reset event initially signalled.
- //This event will be signalled and shall remain so whenever there is data in the queue and
- //it shall be reset as long as queue is empty
- CDataQueue()
- {
- InitializeCriticalSection(&m_csData);
- m_hEvent = CreateEvent(NULL,TRUE,FALSE,NULL);
- };
- //close the event handle
- ~CDataQueue()
- {
- DeleteCriticalSection(&m_csData);
- CloseHandle(m_hEvent);
- };
- //public methods to Push data to queue
- void Push(DataBlock& oNewData)
- {
- EnterCriticalSection(&m_csData);
- //push new element to queue
- m_oQueue.push(oNewData);
- //now that there is atleast one element, set the event
- SetEvent(m_hEvent);
- LeaveCriticalSection(&m_csData);
- };
- //public methods to Pop data from queue
- DataBlock Pop()
- {
- EnterCriticalSection(&m_csData);
- //first get the topmost data block
- DataBlock popData = m_oQueue.front();
- //next remove it from queue
- m_oQueue.pop();
- //now, check for new size.. if no more elements in queue
- //reset the event
- if(!m_oQueue.size())
- ResetEvent(m_hEvent);
- LeaveCriticalSection(&m_csData);
- return popData;
- };
- //helper method to get the event handle
- HANDLE GetEvent(){return m_hEvent;};
- };
- CDataQueue g_oQueue;
- HANDLE g_hExitEvent;
- unsigned __stdcall ProcessData (void * )
- {
- HANDLE hEvents[] = { g_hExitEvent, g_oQueue.GetEvent()};
- DWORD dwRet;
- BOOL bContinue = TRUE;
- while(bContinue)
- {
- dwRet = WaitForMultipleObjects(sizeof(hEvents)/sizeof(hEvents[0]),hEvents,FALSE,INFINITE);
- switch(dwRet)
- {
- case WAIT_OBJECT_0 :
- {
- //exit signalled.. time to quit the thread
- bContinue = FALSE;
- }
- break;
- case WAIT_OBJECT_0 + 1:
- {
- //some data got..
- DataBlock oData = g_oQueue.Pop();
- //echo data to screen
- cout << "Data typed in is .. " << oData.m_szText << "/n";
- }
- break;
- default:break;
- }
- }
- return 0;
- }
- int main(int argc, char* argv[])
- {
- DWORD dwThreadID = 0;
- HANDLE hThread = NULL;
- //create an event to signal worker thread to exit
- g_hExitEvent = CreateEvent(NULL,TRUE,FALSE,NULL);//not signalled initially..
- //spawn a thread for processing the input
- hThread = (HANDLE)_beginthreadex(NULL,0,ProcessData,NULL,0,(unsigned int *)&dwThreadID);
- if(hThread)
- {
- cout << "enter sentence to process /nOR /nenter /"exit/" to quit/n";
- do
- {
- DataBlock oData;
- cin >> oData.m_szText;
- //if exit typed in.. quit
- if(0 == oData.m_szText.compare("exit"))
- break;
- g_oQueue.Push(oData);
- }
- while(1);
- //time to close ..set the exit event
- SetEvent(g_hExitEvent);
- //wait for worker thread to close
- WaitForSingleObject(hThread,INFINITE);
- //close the thread handle
- CloseHandle(hThread);
- }
- CloseHandle(g_hExitEvent);
- return 0;
- }
- template<typename Data>
- class concurrent_queue
- {
- private:
- std::queue<Data> the_queue;
- mutable boost::mutex the_mutex;
- boost::condition_variable the_condition_variable;
- public:
- void push(Data const& data)
- {
- boost::mutex::scoped_lock lock(the_mutex);
- the_queue.push(data);
- lock.unlock();
- the_condition_variable.notify_one();
- }
- bool empty() const
- {
- boost::mutex::scoped_lock lock(the_mutex);
- return the_queue.empty();
- }
- bool try_pop(Data& popped_value)
- {
- boost::mutex::scoped_lock lock(the_mutex);
- if(the_queue.empty())
- {
- return false;
- }
- popped_value=the_queue.front();
- the_queue.pop();
- return true;
- }
- void wait_and_pop(Data& popped_value)
- {
- boost::mutex::scoped_lock lock(the_mutex);
- while(the_queue.empty())
- {
- the_condition_variable.wait(lock);
- }
- popped_value=the_queue.front();
- the_queue.pop();
- }
- };
boost 线程安全队列的更多相关文章
- 使用Condition Variables 实现一个线程安全队列
使用Condition Variables实现一个线程安全队列 测试机: i7-4800MQ .7GHz, logical core, physical core, 8G memory, 256GB ...
- Boost线程库学习笔记
一.创建一个线程 创建线程 boost::thread myThread(threadFun); 需要注意的是:参数可以是函数对象或者函数指针.并且这个函数无参数,并返回void类型. 当一个thre ...
- Boost线程详解
一.创建一个线程 创建线程 boost::thread myThread(threadFun); 需要注意的是:参数可以是函数对象或者函数指针.并且这个函数无参数,并返回void类型. 当一个thre ...
- Boost 线程学习笔记
Bolg转载自:http://www.cnblogs.com/lvdongjie/p/4447193.html 一: 创建线程 #include <iostream> #include & ...
- Linux多线程系列-2-条件变量的使用(线程安全队列的实现)
多线程情况下,往往需要使用互斥变量来实现线程间的同步,实现资源正确共享. linux下使用如下变量和函数 //条件变量 pthread_cond_t int pthread_cond_init (pt ...
- BOOST 线程完全攻略 - 基础篇
http://blog.csdn.net/iamnieo/article/details/2908621 2008-09-10 12:48 9202人阅读 评论(3) 收藏 举报 thread多线程l ...
- BOOST 线程完全攻略 - 扩展 - 可被关闭的线程类
本文假设读者已经基本了解boost线程库的使用方法. boost是个开源工程,线程这一块也在不断完善之中,到现在这个阶段,boost::thread仅仅实现了一个完美的技术框架,但是读者在实际使用中会 ...
- 线程池 队列 synchronized
线程池 BlockingQueue synchronized volatile 本章从线程池到阻塞队列BlockingQueue.从BlockingQueue到synchronized 和 volat ...
- Java线程安全队列BlockingQueue
线程安全队列BlockingQueue 用法跟普通队列没有区别,只是加入了多线程支持. 这里主要说说add和put,以及poll和take的区别: add和put都是用来忘队列里面塞东西的,而poll ...
随机推荐
- TCP/IP、Http大纲
TPC/IP协议是传输层协议,主要解决数据如何在网络中传输,而HTTP是应用层协议,主要解决如何包装数据.关于TCP/IP和HTTP协议的关系,网络有一段比较容易理解的介绍:“我们在传输数据时,可以只 ...
- Linux 快速删除已输入的命令
从输入模式到命令模式: 按”:“到最后一行,再按ctrl+z 就好了 history 显示命令历史列表 ↑(Ctrl+p) 显示上一条命令 ↓(Ctrl+n) 显示下一条命令 !num 执行命令历史列 ...
- 移位操作符 << >> >>>
按位操作符只可用于int类型,其它类型都会转化位int类型在操作,并且只有数值右端的低5位才右用(因为2^5=32) << 左移位操作符,低位补零 >> 右移位操作符,有符 ...
- Codeforces 448C Painting Fence(分治法)
题目链接:http://codeforces.com/contest/448/problem/C 题目大意:n个1* a [ i ] 的木板,把他们立起来,变成每个木板宽为1长为 a [ i ] 的栅 ...
- easyUI拖动课程进课程表
<!DOCTYPE html><html><head> <meta charset="utf-8"> <title>拖动 ...
- HDU1730 Northcott Game 尼姆博弈
Northcott Game Time Limit: 1000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others) T ...
- 怎么用pr(Premiere)给视频添加水印
自己辛辛苦苦剪切的视频,不添加水印,别人肯定会搬走的,作为对视频付出时间的你肯定不想看见这样的事情,下面一起来看看怎么用pr给视频添加水印吧 工具/原料 水印 pr 方法/步骤 首先打 ...
- Redis的优势和特点
Redis的特点: 内存数据库,速度快,也支持数据的持久化,可以将内存中的数据保存在磁盘中,重启的时候可以再次加载进行使用. Redis不仅仅支持简单的key-value类型的数据,同时还提供list ...
- Ubuntu14.04 安装MySQL 及Can‘t connect to local MYSQL server through socket ’/var/run/mysqld/mysqld.sock‘ (2)
今天安装Mysql 按着这个①http://www.cnblogs.com/zhuyp1015/p/3561470.html来安装,却出现了这个问题 卸载又从安装还是有问题, 搜了好久在stackov ...
- 6-1 并行程序模拟 uva210
用到了 deque 和queue 操作说明: queue qu: qu.push(x); int d=qu.front(); qu.pop(); 和栈一样只有push和pop ...