Boost Thread-safe queue

Source: Internet
Author: User

Threadnullmethodsprocessingobjectsignal[CPP]View Plaincopy
  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 would be signalled and shall remain so whenever there was data in the queue and
  23. //it shall be reset as long as the 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 POPs 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 the 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 are:" << 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. }

[CPP]View Plaincopy
  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 Thread-safe queue

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.