首页
Web开发
Windows程序
编程语言
数据库
移动开发
系统相关
微信
其他好文
会员
首页
>
编程语言
> 详细
boost 线程安全队列
时间:
2015-02-16 19:35:17
阅读:
570
评论:
0
收藏:
0
[点我收藏+]
标签:
thread
null
methods
processing
object
signal
[cpp]
view plain
copy
// 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;
}
[cpp]
view plain
copy
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
踩
(
0
)
赞
(
0
)
举报
评论
一句话评论(
0
)
登录后才能评论!
分享档案
更多>
2021年07月29日 (22)
2021年07月28日 (40)
2021年07月27日 (32)
2021年07月26日 (79)
2021年07月23日 (29)
2021年07月22日 (30)
2021年07月21日 (42)
2021年07月20日 (16)
2021年07月19日 (90)
2021年07月16日 (35)
周排行
更多
Spring Cloud 从入门到精通(一)Nacos 服务中心初探
2021-07-29
基础的排序算法
2021-07-29
SpringBoot|常用配置介绍
2021-07-29
关于 .NET 与 JAVA 在 JIT 编译上的一些差异
2021-07-29
C语言常用函数-toupper()将字符转换为大写英文字母函数
2021-07-29
《手把手教你》系列技巧篇(十)-java+ selenium自动化测试-元素定位大法之By class name(详细教程)
2021-07-28
4-1 YAML配置文件 注入 JavaBean中
2021-07-28
【python】 用来将对象持久化的 pickle 模块
2021-07-28
马拉车算法
2021-07-28
用Python进行冒泡排序
2021-07-28
友情链接
兰亭集智
国之画
百度统计
站长统计
阿里云
chrome插件
新版天听网
关于我们
-
联系我们
-
留言反馈
© 2014
mamicode.com
版权所有 联系我们:gaon5@hotmail.com
迷上了代码!