1. // QueueImplementation.cpp : Defines the entry point for the console application.
  2. //
  3. #include "stdafx.h"
  4. #include <windows.h>
  5. #include <iostream>
  6. #include <queue>
  7. #include <string>
  8. #include <process.h>
  9. using namespace std;
  10. struct DataBlock
  11. {
  12. string m_szText;    //sample data
  13. };
  14. class CDataQueue
  15. {
  16. private:
  17. queue<DataBlock>  m_oQueue;   //contains the actual data
  18. CRITICAL_SECTION        m_csData;   //to synchroize access to m_csData among multiple threads
  19. HANDLE                  m_hEvent;   //for signalling presence of absence of data
  20. public:
  21. //create a manual reset event initially signalled.
  22. //This event will be signalled and shall remain so whenever there is data in the queue and
  23. //it shall be reset as long as queue is empty
  24. CDataQueue()
  25. {
  26. InitializeCriticalSection(&m_csData);
  27. m_hEvent = CreateEvent(NULL,TRUE,FALSE,NULL);
  28. };
  29. //close the event handle
  30. ~CDataQueue()
  31. {
  32. DeleteCriticalSection(&m_csData);
  33. CloseHandle(m_hEvent);
  34. };
  35. //public methods to Push data to queue
  36. void Push(DataBlock& oNewData)
  37. {
  38. EnterCriticalSection(&m_csData);
  39. //push new element to queue
  40. m_oQueue.push(oNewData);
  41. //now that there is atleast one element, set the event
  42. SetEvent(m_hEvent);
  43. LeaveCriticalSection(&m_csData);
  44. };
  45. //public methods to Pop data from queue
  46. DataBlock Pop()
  47. {
  48. EnterCriticalSection(&m_csData);
  49. //first get the topmost data block
  50. DataBlock popData = m_oQueue.front();
  51. //next remove it from queue
  52. m_oQueue.pop();
  53. //now, check for new size.. if no more elements in queue
  54. //reset the event
  55. if(!m_oQueue.size())
  56. ResetEvent(m_hEvent);
  57. LeaveCriticalSection(&m_csData);
  58. return popData;
  59. };
  60. //helper method to get the event handle
  61. HANDLE GetEvent(){return m_hEvent;};
  62. };
  63. CDataQueue g_oQueue;
  64. HANDLE     g_hExitEvent;
  65. unsigned __stdcall ProcessData (void * )
  66. {
  67. HANDLE hEvents[] = { g_hExitEvent, g_oQueue.GetEvent()};
  68. DWORD dwRet;
  69. BOOL bContinue = TRUE;
  70. while(bContinue)
  71. {
  72. dwRet = WaitForMultipleObjects(sizeof(hEvents)/sizeof(hEvents[0]),hEvents,FALSE,INFINITE);
  73. switch(dwRet)
  74. {
  75. case WAIT_OBJECT_0 :
  76. {
  77. //exit signalled.. time to quit the thread
  78. bContinue = FALSE;
  79. }
  80. break;
  81. case WAIT_OBJECT_0 + 1:
  82. {
  83. //some data got..
  84. DataBlock oData = g_oQueue.Pop();
  85. //echo data to screen
  86. cout << "Data typed in is .. " << oData.m_szText << "/n";
  87. }
  88. break;
  89. default:break;
  90. }
  91. }
  92. return 0;
  93. }
  94. int main(int argc, char* argv[])
  95. {
  96. DWORD dwThreadID = 0;
  97. HANDLE hThread = NULL;
  98. //create an event to signal worker thread to exit
  99. g_hExitEvent = CreateEvent(NULL,TRUE,FALSE,NULL);//not signalled initially..
  100. //spawn a thread for processing the input
  101. hThread = (HANDLE)_beginthreadex(NULL,0,ProcessData,NULL,0,(unsigned int *)&dwThreadID);
  102. if(hThread)
  103. {
  104. cout << "enter sentence to process /nOR /nenter /"exit/" to quit/n";
  105. do
  106. {
  107. DataBlock oData;
  108. cin >> oData.m_szText;
  109. //if exit typed in.. quit
  110. if(0 == oData.m_szText.compare("exit"))
  111. break;
  112. g_oQueue.Push(oData);
  113. }
  114. while(1);
  115. //time to close ..set the exit event
  116. SetEvent(g_hExitEvent);
  117. //wait for worker thread to close
  118. WaitForSingleObject(hThread,INFINITE);
  119. //close the thread handle
  120. CloseHandle(hThread);
  121. }
  122. CloseHandle(g_hExitEvent);
  123. return 0;
  124. }
  1. template<typename Data>
  2. class concurrent_queue
  3. {
  4. private:
  5. std::queue<Data> the_queue;
  6. mutable boost::mutex the_mutex;
  7. boost::condition_variable the_condition_variable;
  8. public:
  9. void push(Data const& data)
  10. {
  11. boost::mutex::scoped_lock lock(the_mutex);
  12. the_queue.push(data);
  13. lock.unlock();
  14. the_condition_variable.notify_one();
  15. }
  16. bool empty() const
  17. {
  18. boost::mutex::scoped_lock lock(the_mutex);
  19. return the_queue.empty();
  20. }
  21. bool try_pop(Data& popped_value)
  22. {
  23. boost::mutex::scoped_lock lock(the_mutex);
  24. if(the_queue.empty())
  25. {
  26. return false;
  27. }
  28. popped_value=the_queue.front();
  29. the_queue.pop();
  30. return true;
  31. }
  32. void wait_and_pop(Data& popped_value)
  33. {
  34. boost::mutex::scoped_lock lock(the_mutex);
  35. while(the_queue.empty())
  36. {
  37. the_condition_variable.wait(lock);
  38. }
  39. popped_value=the_queue.front();
  40. the_queue.pop();
  41. }
  42. };

boost 线程安全队列的更多相关文章

  1. 使用Condition Variables 实现一个线程安全队列

    使用Condition Variables实现一个线程安全队列 测试机: i7-4800MQ .7GHz, logical core, physical core, 8G memory, 256GB ...

  2. Boost线程库学习笔记

    一.创建一个线程 创建线程 boost::thread myThread(threadFun); 需要注意的是:参数可以是函数对象或者函数指针.并且这个函数无参数,并返回void类型. 当一个thre ...

  3. Boost线程详解

    一.创建一个线程 创建线程 boost::thread myThread(threadFun); 需要注意的是:参数可以是函数对象或者函数指针.并且这个函数无参数,并返回void类型. 当一个thre ...

  4. Boost 线程学习笔记

    Bolg转载自:http://www.cnblogs.com/lvdongjie/p/4447193.html 一: 创建线程 #include <iostream> #include & ...

  5. Linux多线程系列-2-条件变量的使用(线程安全队列的实现)

    多线程情况下,往往需要使用互斥变量来实现线程间的同步,实现资源正确共享. linux下使用如下变量和函数 //条件变量 pthread_cond_t int pthread_cond_init (pt ...

  6. BOOST 线程完全攻略 - 基础篇

    http://blog.csdn.net/iamnieo/article/details/2908621 2008-09-10 12:48 9202人阅读 评论(3) 收藏 举报 thread多线程l ...

  7. BOOST 线程完全攻略 - 扩展 - 可被关闭的线程类

    本文假设读者已经基本了解boost线程库的使用方法. boost是个开源工程,线程这一块也在不断完善之中,到现在这个阶段,boost::thread仅仅实现了一个完美的技术框架,但是读者在实际使用中会 ...

  8. 线程池 队列 synchronized

    线程池 BlockingQueue synchronized volatile 本章从线程池到阻塞队列BlockingQueue.从BlockingQueue到synchronized 和 volat ...

  9. Java线程安全队列BlockingQueue

    线程安全队列BlockingQueue 用法跟普通队列没有区别,只是加入了多线程支持. 这里主要说说add和put,以及poll和take的区别: add和put都是用来忘队列里面塞东西的,而poll ...

随机推荐

  1. bootstrap----几个插件网址

    1.SweetAlert (弹出框):https://github.com/t4t5/sweetalert 2.SweetAlert2 (弹出框):https://github.com/limonte ...

  2. jquery----语法扩展(导入js文件)

    简单使用 第一步,新建js文件 第二步,在js文件中添加 $.extend({ "GDP": function () { console.log("哈哈哈哈") ...

  3. Codeforces 407B Long Path(好题 DP+思维)

    题目链接:http://codeforces.com/problemset/problem/407/B 题目大意:一共n+1个房间,一个人从1走到n+1,每次经过房间都会留下一个标记,每个房间有两扇门 ...

  4. git填坑笔记

    Counting objects: 3, done.Writing objects: 100% (3/3), 203 bytes | 0 bytes/s, done.Total 3 (delta 0) ...

  5. 2017-2018-2 20155225《网络对抗技术》实验八 Web基础

    2017-2018-2 20155225<网络对抗技术>实验八 Web基础 1.Web前端HTML 输入命令apachectl start打开apahce,并使用netstat -aptn ...

  6. DBMS_OUTPUT包学习

    DBMS_OUTPUT包中的其他方法和函数的用法,所以这次特地来研究一下. 先简单的讲解一下这个包的所有procedure的含义及作用:  -----------------------     1. ...

  7. C#的基础

    一:Ref和Out 的区别: 1.使用ref型参数时,传入的参数必须先被初始化.对out而言,必须在方法中对其完成初始化. 2.使用ref和out时,在方法的参数和执行方法时,都要加Ref或Out关键 ...

  8. 【C++ Primer 第13章】3. 交换操作

    交换操作 class HasPtr { friend void swap(HasPtr &rhs, HasPtr &yhs); //其他成员定义 }; void swap(HasPtr ...

  9. codeforces 750D New Year and Fireworks【DFS】

    题意:烟花绽放时分为n层,每层会前进ti格,当进入下一层是向左右45°分开前进. 问在网格中,有多少网格至少被烟花经过一次? 题解:最多30层,每层最多前进5格,烟花的活动半径最大为150,每一层的方 ...

  10. #3 Codeforces-865C Gotta Go Fast(期望dp)

    题意:一个游戏一共有n个关卡,对于第i关,用a[i]时间通过的概率为p[i],用b[i]通过的时间为1-p[i],每通过一关后可以选择继续下一关或者时间清0并从第一关开始,先要求通过所有关卡的时间和不 ...