tbb 线程安全concurrent_queue的性能
tbb实现了线程安全的queue,这样程序员既可以不用和那些lock,mutex,criticalsection打交道,又大大提高性能,太给力了。。比较的结果见代码中的注释。结果可以看出代码足足少一半,性能足足生一倍,诱人!
#include <Windows.h>
#include <deque>
#include <iostream>
#include <process.h>
#include <tbb\concurrent_queue.h> using namespace std; static CRITICAL_SECTION s_cs;
static HANDLE s_mutex = CreateMutex(NULL, FALSE, NULL);
static deque<int> s_queue; const static int LOOP_COUNT_ = 3000000; static bool s_running = true; static tbb::concurrent_queue<int> s_tbb_queue;
/*
1. not tbb, not criticalsection, is mutex, 11656
2. not tbb, is criticalsection, not mutex, 8172
3. is tbb, not criticalsection, not mutex, 3828
*/ /*下面的两个宏是开关*/
#define _MUTEX_TEST_ ""
#define _USE_TBB_ ""
#ifdef _USE_TBB_
void thread1_(void* dummy){
LONGLONG start = GetTickCount();
for (int i = 0; i < LOOP_COUNT_; i ++){
s_tbb_queue.push(i);
}
cout << GetTickCount() - start << endl;
Sleep(20000);
s_running = false;
} void thread2_(void* dummy){
int i = 0;
int k;
while (s_running){
if (s_tbb_queue.try_pop(k))
{
i ++;
} else{
Sleep(1);
} } cout << "consume " << i << endl; }
#else #ifdef _MUTEX_TEST_
void thread1_(void* dummy){
LONGLONG start = GetTickCount();
for (int i = 0; i < LOOP_COUNT_; i ++){
WaitForSingleObject(s_mutex, INFINITE);
s_queue.push_back(i);
ReleaseMutex(s_mutex);
}
cout << GetTickCount() - start << endl;
Sleep(20000);
s_running = false;
} void thread2_(void* dummy){
int i = 0;
while (s_running){
WaitForSingleObject(s_mutex, INFINITE);
bool bEmpty = s_queue.empty();
if (!bEmpty){
int res = s_queue.front();
s_queue.pop_front();
}
ReleaseMutex(s_mutex); if (!bEmpty){
i ++;
} else {
Sleep(1);
} } cout << "consume " << i << endl; } #else
void thread1_(void* dummy){
LONGLONG start = GetTickCount();
for (int i = 0; i < LOOP_COUNT_; i ++){
EnterCriticalSection(&s_cs);
s_queue.push_back(i);
LeaveCriticalSection(&s_cs);
}
cout << GetTickCount() - start << endl;
Sleep(20000);
s_running = false;
} void thread2_(void* dummy){
int i = 0;
while (s_running){
EnterCriticalSection(&s_cs);
bool bEmpty = s_queue.empty();
if (!bEmpty){
int res = s_queue.front();
s_queue.pop_front();
}
LeaveCriticalSection(&s_cs); if (!bEmpty){
i ++;
} else {
Sleep(1);
} }
cout << "consume " << i << endl;
} #endif
#endif void main(){
InitializeCriticalSection(&s_cs);
_beginthread(thread2_, 0, NULL);
_beginthread(thread2_, 0, NULL);//2 consumer
_beginthread(thread1_, 0, NULL); while(s_running){
Sleep(10);
}
}
tbb 线程安全concurrent_queue的性能的更多相关文章
- 【转】线程及同步的性能 - 线程池 / ThreadPoolExecutors / ForkJoinPool
线程池和ThreadPoolExecutors 虽然在程序中可以直接使用Thread类型来进行线程操作,但是更多的情况是使用线程池,尤其是在Java EE应用服务器中,一般会使用若干个线程池来处理来自 ...
- 通过 Javacore 诊断线程挂起等性能问题
http://www.ibm.com/developerworks/cn/websphere/library/techarticles/1406_tuzy_javacore/1406_tuzy_jav ...
- 线程及同步的性能 – 线程池/ ThreadPoolExecutors/ ForkJoinPool
线程池和ThreadPoolExecutors 虽然在程序中可以直接使用Thread类型来进行线程操作,但是更多的情况是使用线程池,尤其是在Java EE应用服务器中,一般会使用若干个线程池来处理来自 ...
- [Java Performance] 线程及同步的性能之线程池/ThreadPoolExecutors/ForkJoinPool
线程池和ThreadPoolExecutors 虽然在程序中可以直接使用Thread类型来进行线程操作,但是更多的情况是使用线程池,尤其是在Java EE应用服务器中,一般会使用若干个线程池来处理 ...
- Java 线程与同步的性能优化
本文探讨的主题是,如何挖掘出Java线程和同步设施的最大性能. 1.线程池与ThreadPoolExecutor 1)线程池与ThreadPoolExecutor 线程池的实现可能有所不同,但基本概念 ...
- (转)通过 Javacore 诊断线程挂起等性能问题
原文:https://www.ibm.com/developerworks/cn/websphere/library/techarticles/1406_tuzy_javacore/1406_tuzy ...
- python3 线程 threading.Thread GIL性能详解(2.3)
python3 线程 threading 最基础的线程的使用 import threading, time value = 0 lock = threading.Lock() def change(n ...
- Java性能分析之线程栈详解与性能分析
Java性能分析之线程栈详解 Java性能分析迈不过去的一个关键点是线程栈,新的性能班级也讲到了JVM这一块,所以本篇文章对线程栈进行基础知识普及以及如何对线程栈进行性能分析. 基本概念 线程堆栈也称 ...
- Android开发之Java集合类性能分析
对于Android开发者来说深入了解Java的集合类很有必要主要是从Collection和Map接口衍生出来的,目前主要提供了List.Set和 Map这三大类的集合,今天Android吧(ard8. ...
随机推荐
- stm32之watchdog
在嵌入式系统中,由于MCU的工作常常受到来自外界电磁场的干扰,造成程序的跑飞,而陷入死循环,程序的正常运行被打断,由单片机控制的系统无法继续工作,会造成整个系统陷入停滞状态,发送不可预料的后果,所以出 ...
- powerMock比easyMock和Mockito更强大(转)
powerMock是基于easyMock或Mockito扩展出来的增强版本,所以powerMock分两种类型,如果你习惯于使用easyMock的,那你就下载基于easyMock的powerMock,反 ...
- keepavlied一些参数
priority 表示优先级 virtual_ipaddress 虚拟的IP地址(VIP) delay_loop 每个2秒检查一次real_server状态 notify_down 检测到服务down ...
- 浙江大学PAT上机题解析之3-04. 一元多项式的乘法与加法运算
设计函数分别求两个一元多项式的乘积与和. 输入格式说明: 输入分2行,每行分别先给出多项式非零项的个数,再以指数递降方式输入一个多项式非零项系数和指数(绝对值均为不超过1000的整数).数字间以空格分 ...
- 项目优化经验分享(六)SVN冲突和处理
上一篇博客我们分享了新增需求的确定思想<站在全局看问题>.今天我们来分享项目开发中SVN冲突的解决经验:SVN冲突和处理! 引言 开发过项目的人都知道,公司开发一个项目都会使用到版本号控制 ...
- 【linux】U-BOOT与linux kernel通信: struct tag
欢迎转载,转载时需保留作者信息. 邮箱:tangzhongp@163.com 博客园地址:http://www.cnblogs.com/embedded-tzp Csdn博客地址:http://b ...
- WCF技术剖析之五:利用ASP.NET兼容模式创建支持会话(Session)的WCF服务
原文:WCF技术剖析之五:利用ASP.NET兼容模式创建支持会话(Session)的WCF服务 在<基于IIS的WCF服务寄宿(Hosting)实现揭秘>中,我们谈到在采用基于IIS(或者 ...
- 读取中兴3G告警log告警文件到集合
1.文件格式 ALARM_ID=102305_404205 EVENT_TIME=-- :: NOTIFICATION_TYPE= MANAGED_OBJECT_INSTANCE=NodeId=,Bs ...
- document.body的一些用法以及js中的常见问题
document.body的一些用法以及js中的常见问题 网页可见区域宽: document.body.clientWidth; 网页可见区域高: document.body.clientHeight ...
- c# 未能载入文件或程序集
近期做项目时碰到这个问题了.goole.百度了半天,整理了下面几种可能: DLL文件名称与载入时的DLL文件名称不一致, DLL文件根本不存在,即出现丢失情况, 载入DLL路径错误,即DLL文件存在, ...