- 浏览: 25454 次
- 性别:
- 来自: 深圳
最新评论
// BlockingQueue.h: interface for the CBlockingQueue class. // ////////////////////////////////////////////////////////////////////// #if !defined(AFX_BLOCKINGQUEUE_H__E6C614E8_4A5D_4D18_A38D_845018DA75B6__INCLUDED_) #define AFX_BLOCKINGQUEUE_H__E6C614E8_4A5D_4D18_A38D_845018DA75B6__INCLUDED_ #if _MSC_VER > 1000 #pragma once #endif // _MSC_VER > 1000 #include <windows.h> #include <LIST> #include <vector> #include <STRING> using namespace std; class CBlockingQueue { public: CBlockingQueue(int size); virtual ~CBlockingQueue(); private: CBlockingQueue(); HANDLE m_MssageNullEvent; HANDLE m_synSignal; int m_size; list<string> m_msgs; public: string Dequeue(); void Enqueue(string msg); }; #endif // !defined(AFX_BLOCKINGQUEUE_H__E6C614E8_4A5D_4D18_A38D_845018DA75B6__INCLUDED_)
// BlockingQueue.cpp: implementation of the CBlockingQueue class. // ////////////////////////////////////////////////////////////////////// #include "stdafx.h" #include "BlockingQueue.h" #include <afxcom_.h> ////////////////////////////////////////////////////////////////////// // Construction/Destruction ////////////////////////////////////////////////////////////////////// CBlockingQueue::CBlockingQueue() { } CBlockingQueue::CBlockingQueue(int ssize) { m_size=ssize; m_synSignal=CreateSemaphore(NULL,1,1,"m_synSignal"); m_MssageNullEvent=CreateEvent(NULL,TRUE,TRUE,"m_MssageNullEvent"); } CBlockingQueue::~CBlockingQueue() { } string CBlockingQueue::Dequeue() { string msg=""; WaitForSingleObject(m_synSignal,INFINITE); while(0==m_msgs.size()) { bool breset=ResetEvent(m_MssageNullEvent); ReleaseSemaphore(m_synSignal,1,NULL); DWORD nret=WaitForSingleObject(m_MssageNullEvent,INFINITE); //ASSERT(WAIT_TIMEOUT!=); } string temp=*m_msgs.begin(); m_msgs.pop_front(); SetEvent(m_MssageNullEvent); ReleaseSemaphore(m_synSignal,1,NULL); return temp; } void CBlockingQueue::Enqueue(string msg) { WaitForSingleObject(m_synSignal,INFINITE); int a=m_msgs.size(); while(m_size==m_msgs.size()) { bool bret= ResetEvent(m_MssageNullEvent); ReleaseSemaphore(m_synSignal,1,NULL); DWORD nret= WaitForSingleObject(m_MssageNullEvent,INFINITE); } m_msgs.push_back(msg); ReleaseSemaphore(m_synSignal,1,NULL); if (1==m_msgs.size()) SetEvent(m_MssageNullEvent); }
以上不具有普遍性,更合理的设计如下:
将 同步内核对象抽象出来 成为 CMonitor
// Monitor.h: interface for the CMonitor class. // ////////////////////////////////////////////////////////////////////// #if !defined(AFX_MONITOR_H__26A4800D_6F3C_41BF_97AC_1D20860517AC__INCLUDED_) #define AFX_MONITOR_H__26A4800D_6F3C_41BF_97AC_1D20860517AC__INCLUDED_ #if _MSC_VER > 1000 #pragma once #endif // _MSC_VER > 1000 #include <windows.h> class CMonitor { public: CMonitor(); virtual ~CMonitor(); private: HANDLE m_h[2]; DWORD m_lastThreadId; public: void wait(DWORD timeout); bool pulse(); void Enter(); void Exit(); }; #endif // !defined(AFX_MONITOR_H__26A4800D_6F3C_41BF_97AC_1D20860517AC__INCLUDED_)
// Monitor.cpp: implementation of the CMonitor class. // ////////////////////////////////////////////////////////////////////// #include "stdafx.h" #include "Monitor.h" #include <exception> ////////////////////////////////////////////////////////////////////// // Construction/Destruction ////////////////////////////////////////////////////////////////////// CMonitor::CMonitor() { m_h[0]=CreateEvent(NULL,TRUE,TRUE,"h1"); m_h[1]=CreateEvent(NULL,TRUE,TRUE,"h2"); m_lastThreadId=0; } CMonitor::~CMonitor() { } void CMonitor::Enter() { m_lastThreadId=::GetCurrentThreadId(); WaitForSingleObject(m_h[0],INFINITE);//ResetEvent(); } void CMonitor::Exit() { m_lastThreadId=NULL; SetEvent(m_h[0]); SetEvent(m_h[1]); } bool CMonitor::pulse() { if (!(m_lastThreadId!=NULL || m_lastThreadId!=::GetCurrentThreadId())) { throw exception("The wait could only be excuted by the monitor owner!"); } SetEvent(m_h[1]); return true; } void CMonitor::wait( DWORD timeout ) { if (!(m_lastThreadId!=NULL || m_lastThreadId!=::GetCurrentThreadId())) { throw exception("The wait could only be excuted by the monitor owner!"); } m_lastThreadId=NULL; ResetEvent(m_h[1]); WaitForSingleObject(m_h[1],timeout); SetEvent(m_h[0]); }
那么新的 BlockingQueue代码如下:
// BlockingQueue.h: interface for the CBlockingQueue class. // ////////////////////////////////////////////////////////////////////// #if !defined(AFX_BLOCKINGQUEUE_H__E6C614E8_4A5D_4D18_A38D_845018DA75B6__INCLUDED_) #define AFX_BLOCKINGQUEUE_H__E6C614E8_4A5D_4D18_A38D_845018DA75B6__INCLUDED_ #if _MSC_VER > 1000 #pragma once #endif // _MSC_VER > 1000 #include <windows.h> #include <LIST> #include <vector> #include <STRING> #include "Monitor.h" using namespace std; class CBlockingQueue { public: CBlockingQueue(int size); virtual ~CBlockingQueue(); private: CBlockingQueue(); int m_size; list<string> m_msgs; CMonitor m_monitor; public: string Dequeue(); void Enqueue(string msg); }; #endif // !defined(AFX_BLOCKINGQUEUE_H__E6C614E8_4A5D_4D18_A38D_845018DA75B6__INCLUDED_)
// BlockingQueue.cpp: implementation of the CBlockingQueue class. // ////////////////////////////////////////////////////////////////////// #include "stdafx.h" #include "BlockingQueue.h" #include <afxcom_.h> ////////////////////////////////////////////////////////////////////// // Construction/Destruction ////////////////////////////////////////////////////////////////////// CBlockingQueue::CBlockingQueue() { } CBlockingQueue::CBlockingQueue(int ssize) { m_size=ssize; } CBlockingQueue::~CBlockingQueue() { } string CBlockingQueue::Dequeue() { string msg=""; m_monitor.Enter(); { while(0==m_msgs.size()) { m_monitor.wait(INFINITE); } msg=*m_msgs.begin(); m_msgs.pop_front(); if ((m_size-1)==m_msgs.size()) m_monitor.pulse(); } m_monitor.Exit(); return msg; } void CBlockingQueue::Enqueue(string msg) { m_monitor.Enter(); { int a=m_msgs.size(); while(m_size==m_msgs.size()) { m_monitor.wait(INFINITE); } m_msgs.push_back(msg); if (1==m_msgs.size()) m_monitor.pulse(); } m_monitor.Exit(); }
发表评论
-
网络编程——一些思考
2013-05-09 15:07 4901. 在学习网络编程的时候,我通过网上的了解,买了不少书, ... -
centos中编译log4cxx
2013-03-18 10:10 1535log4cxx-0.10.0日志中文乱码 log4cxx ... -
linux在用户程序中如何向操作系统发送按键事件
2013-01-23 19:09 2484转自:http://blog.csdn.net/xian ... -
为什么linux下多线程编程,每次执行结果都不一样
2013-01-03 21:41 1162#include <pthread.h> ... -
27种设计模式C++实现——单例模式
2012-09-25 22:02 01. 单例模式 -
27种设计模式C++实现——原始模型模式
2012-09-25 22:01 6561. 克隆接口 2. 具体实现者类 -
27种设计模式C++实现——建造者模式
2012-09-25 21:59 10061. 指导者类 2. 抽象建造者类 3. 具体建造者类 ... -
27种设计模式C++实现——抽象工厂
2012-09-25 21:57 11281. 抽象产品类 2. 具体产品类 3. 抽象工厂 4. ... -
27种设计模式C++实现——工厂方法
2012-09-25 21:55 6131. 抽象产品类 2. 具体产品类 3. 工厂接口 4. ... -
27种设计模式C++实现——简单工厂
2012-09-25 21:54 624简单工厂 1. 抽象产品类 2. 具体产品类 3. ... -
面向对象编程<继承覆盖>之——C++
2012-09-23 21:39 665C++面向对象继承,虚方法,类似于指针..... ... -
windows进程同步
2012-09-21 15:40 8691. 进程同步的思想很简单 操作系统所有进程,都是内核 ... -
C内存对齐详解
2012-09-18 17:05 595一、什么是对齐,以及为什么要对齐: 1. 现代计算机中内存空 ... -
C++
2012-09-18 11:30 01. 学会数据分层,例如串口指令,与硬件业务分离 2. 学会 ... -
Java与C++内存回收浅析
2012-09-17 11:12 0java与C++内存回收浅析 内存分配结构 ... -
函数如何返回struct或class对象
2012-07-16 16:28 551所有的C、C++教科书都警 ... -
MFC Activex与JavaScript的接口交互
2012-06-18 15:06 1225在Activex的应用中与网页的JavaScript的交互必不 ...
相关推荐
类似java BlockingQueue,C++写的,支持Windows与Linux。
简单实现BlockingQueue,BlockingQueue源码详解
线程----BlockingQueue 的介绍说明
在新增的Concurrent包中,BlockingQueue很好的解决了多线程中,如何高效安全“传输”数据的问题。通过这些高效并且线程安全的队列类,为我们快速搭建高质量的多线程程序带来极大的便利。本文详细介绍了BlockingQueue...
BlockingQueue java 的工具类,初次要用于消费者,生产者的同步问题。
主要介绍了Java多线程Queue、BlockingQueue和使用BlockingQueue实现生产消费者模型方法解析,涉及queue,BlockingQueue等有关内容,具有一定参考价值,需要的朋友可以参考。
定义全局线程池,将用户的请求放入自定义队列中,排队等候线程调用,等待超时则自动取消该任务,实现超时可取消的异步任务
这个demo主要讲解了BlockingQueue的使用希望可以帮户需要的同学.
主要介绍了java并发学习之BlockingQueue实现生产者消费者详解,具有一定参考价值,需要的朋友可以了解下。
BlockingQueue是一种特殊的Queue,若BlockingQueue是空的,从BlockingQueue取东西的操作将会被阻断进入等待状态直到BlocingkQueue进了新货才会被唤醒,下面是用BlockingQueue来实现Producer和Consumer的例子
14-阻塞队列BlockingQueue实战及其原理分析二.pdf
主要介绍了Java多线程 BlockingQueue实现生产者消费者模型详解,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友可以参考下
zthread patch,主要解决编译和BlockingQueue的问题。 使用方法: cd zthread_xxx_dir patch -p1 < zthread.patch
源码:BlockingQueue实现生产者消费者模式→ 输出结果截图 1. Queue接口 – 队列 public interface Queue extends Collection Collection的子接口,表示队列FIFO(First In First Out) 常用方法: (1)抛出异常...
10、阻塞队列BlockingQueue实战及其原理分析_
主要介绍了详解Java阻塞队列(BlockingQueue)的实现原理,阻塞队列是Java util.concurrent包下重要的数据结构,有兴趣的可以了解一下
在新增的Concurrent包中,BlockingQueue很好的解决了多线程中,如何高效安全“传输”数据的问题。通过这些高效并且线程安全的队列类,为我们快速搭建高质量的多线程程序带来极大的便利。本文简要介绍下BlockingQueue...
java中线程队列BlockingQueue的用法
BlockingQueue支持两个附加操作的Queue:1)当Queue为空时,获取元素线程被阻塞直到Queue变为非空;2)当Queue满时,添加元素线程被阻塞直到Queue不满。BlockingQueue不允许元素为null,如果入队一个null元素,会抛...
本篇文章小编为大家介绍,基于java中BlockingQueue的使用介绍。需要的朋友参考下