- #include "stdafx.h"
- #include <windows.h>
- #include <iostream>
- #include <queue>
- #include <string>
- #include <process.h>
- using namespace std;
- struct DataBlock
- {
- string m_szText;
- };
- class CDataQueue
- {
- private:
- queue<DataBlock> m_oQueue;
- CRITICAL_SECTION m_csData;
- HANDLE m_hEvent;
- public:
-
-
-
- CDataQueue()
- {
- InitializeCriticalSection(&m_csData);
- m_hEvent = CreateEvent(NULL,TRUE,FALSE,NULL);
- };
-
- ~CDataQueue()
- {
- DeleteCriticalSection(&m_csData);
- CloseHandle(m_hEvent);
- };
-
- void Push(DataBlock& oNewData)
- {
- EnterCriticalSection(&m_csData);
-
- m_oQueue.push(oNewData);
-
- SetEvent(m_hEvent);
- LeaveCriticalSection(&m_csData);
- };
-
- DataBlock Pop()
- {
- EnterCriticalSection(&m_csData);
-
- DataBlock popData = m_oQueue.front();
-
- m_oQueue.pop();
-
-
- if(!m_oQueue.size())
- ResetEvent(m_hEvent);
- LeaveCriticalSection(&m_csData);
- return popData;
- };
-
- 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 :
- {
-
- bContinue = FALSE;
- }
- break;
- case WAIT_OBJECT_0 + 1:
- {
-
- DataBlock oData = g_oQueue.Pop();
-
- 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;
-
- g_hExitEvent = CreateEvent(NULL,TRUE,FALSE,NULL);
-
- 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(0 == oData.m_szText.compare("exit"))
- break;
- g_oQueue.Push(oData);
- }
- while(1);
-
- SetEvent(g_hExitEvent);
-
- WaitForSingleObject(hThread,INFINITE);
-
- 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 线程安全队列
原文:http://www.cnblogs.com/rainbowzc/p/4294452.html