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. ...
随机推荐
- Hadoop源码解析之: TextInputFormat如何处理跨split的行
我们知道hadoop将数据给到map进行处理前会使用InputFormat对数据进行两方面的预处理: 对输入数据进行切分,生成一组split,一个split会分发给一个mapper进行处理. 针对每个 ...
- SGU 106 The Equation 扩展欧几里得应用
Sol:线性不定方程+不等式求解 证明的去搜下别人的证明就好了...数学题. #include <algorithm> #include <cstdio> #include & ...
- 四、Nginx负载均衡upstream
user www; worker_processes ; error_log /usr/local/nginx/logs/error.log crit; pid /usr/local/nginx/lo ...
- 转载python并行运算实例
Python的并发处理能力臭名昭著.先撇开线程以及GIL方面的问题不说,我觉得多线程问题的根源不在技术上而在于理念.大部分关于Pyhon线程和多进程的资料虽然都很不错,但却过于细节.这些资料讲的都是虎 ...
- 知识点1-1:什么是ASP.NET MVC
ASP.NET MVC是微软.NET平台上的一个Web开发框架,它为开发者提供了一种构建结构良好的Web应用程序的方式.自2007年首次公布预览以来,作为Web Form的替代品,ASP.NET MV ...
- api 跳转规则
api 配置: <Context docBase="zjzc-web-api" path="/api" reloadable="false&qu ...
- POJ 3986 Math teacher's homework
题目 给出\(n,m_1,m_2,...,m_n\),求\(x_1 xor x_2 xor ... xor x_n=k (0 \leq x_i \leq m_i)\)的解的数量.二进制位数小于\(32 ...
- Android-onInterceptTouchEvent()和onTouchEvent()总结
老实说,这两个小东东实在是太麻烦了,很不好懂,我自己那api文档都头晕,在网上找到很多资料,才知道是怎么回事,这里总结一下,记住这个原则就会很清楚了: 1.onInterceptTouchEvent( ...
- Python 对Twitter中指定话题的被转载Tweet数量的频谱分析
CODE: #!/usr/bin/python # -*- coding: utf-8 -*- ''' Created on 2014-7-10 @author: guaguastd @name: r ...
- CodeForces Round #173 (282E) - Sausage Maximization 字典树
练习赛的时候这道题死活超时....想到了高位确定后..低位不能对高位产生影响..并且高位要尽可能的为1..就是想不出比较好的方法了实现... 围观大神博客..http://www.cnblogs.co ...