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的性能的更多相关文章

  1. 【转】线程及同步的性能 - 线程池 / ThreadPoolExecutors / ForkJoinPool

    线程池和ThreadPoolExecutors 虽然在程序中可以直接使用Thread类型来进行线程操作,但是更多的情况是使用线程池,尤其是在Java EE应用服务器中,一般会使用若干个线程池来处理来自 ...

  2. 通过 Javacore 诊断线程挂起等性能问题

    http://www.ibm.com/developerworks/cn/websphere/library/techarticles/1406_tuzy_javacore/1406_tuzy_jav ...

  3. 线程及同步的性能 – 线程池/ ThreadPoolExecutors/ ForkJoinPool

    线程池和ThreadPoolExecutors 虽然在程序中可以直接使用Thread类型来进行线程操作,但是更多的情况是使用线程池,尤其是在Java EE应用服务器中,一般会使用若干个线程池来处理来自 ...

  4. [Java Performance] 线程及同步的性能之线程池/ThreadPoolExecutors/ForkJoinPool

    线程池和ThreadPoolExecutors   虽然在程序中可以直接使用Thread类型来进行线程操作,但是更多的情况是使用线程池,尤其是在Java EE应用服务器中,一般会使用若干个线程池来处理 ...

  5. Java 线程与同步的性能优化

    本文探讨的主题是,如何挖掘出Java线程和同步设施的最大性能. 1.线程池与ThreadPoolExecutor 1)线程池与ThreadPoolExecutor 线程池的实现可能有所不同,但基本概念 ...

  6. (转)通过 Javacore 诊断线程挂起等性能问题

    原文:https://www.ibm.com/developerworks/cn/websphere/library/techarticles/1406_tuzy_javacore/1406_tuzy ...

  7. python3 线程 threading.Thread GIL性能详解(2.3)

    python3 线程 threading 最基础的线程的使用 import threading, time value = 0 lock = threading.Lock() def change(n ...

  8. Java性能分析之线程栈详解与性能分析

    Java性能分析之线程栈详解 Java性能分析迈不过去的一个关键点是线程栈,新的性能班级也讲到了JVM这一块,所以本篇文章对线程栈进行基础知识普及以及如何对线程栈进行性能分析. 基本概念 线程堆栈也称 ...

  9. Android开发之Java集合类性能分析

    对于Android开发者来说深入了解Java的集合类很有必要主要是从Collection和Map接口衍生出来的,目前主要提供了List.Set和 Map这三大类的集合,今天Android吧(ard8. ...

随机推荐

  1. C#文件上传和文件下载

    #region 文件上传 private void UpLoadFile(string fileName, string fileNamePath, string uriString) { ); if ...

  2. grunt getHTML

    var Base = require( "../common/base" ) , HandlerBase = require( "../common/handlerBas ...

  3. 使用链表实现队列------《数据结构与算法分析-C语言描述》

    经过ubuntu的gcc验证 一.头文件 que_link.h #ifndef _QUE_LINK_H_ #define _QUE_LINK_H_ struct que_record; typedef ...

  4. 多图片/文件上传 - SwfUpload/PlUpload

    <文件上传利器SWFUpload使用指南> <前端上传组件Plupload使用指南>

  5. SharePoint 2013的100个新功能之搜索(二)

    一:名称建议 人员搜索中新的“名称建议”功能,微软引入了一种简单.直观的方式来根据名称找到用户.输入一个或多个字符,查看全部以其开头的名称,在所有的用户描述数据库都可用,在人员索引中也因此一样可用.该 ...

  6. ZJUT 1423 地下迷宫(期望DP&高斯消元)

    地下迷宫 Time Limit:1000MS  Memory Limit:32768K Description: 由于山体滑坡,DK被困在了地下蜘蛛王国迷宫.为了抢在DH之前来到TFT,DK必须尽快走 ...

  7. iOS 多线程编程之Grand Central Dispatch(GCD)

    介绍: Grand Central Dispatch 简称(GCD)是苹果公司开发的技术,以优化的应用程序支持多核心处理器和其它的对称多处理系统的系统.这建立在任务并行运行的线程池模式的基础上的. 它 ...

  8. java之redis篇(spring-data-redis整合) (转)

    redis的知识:官网 1,利用spring-data-redis整合 项目使用的pom.xml: <project xmlns="http://maven.apache.org/PO ...

  9. IT大数据服务管理高级课程(IT服务,大数据,云计算,智能城市)

    个人简历 金石先生是马克思主义中国化的研究学者,上海财经大学经济学和管理学硕士,中国民主建国会成员,中国特色社会主义人文科技管理哲学的理论奠基人之一.金石先生博学多才,对问题有独到见解.专于工作且乐于 ...

  10. eclipse之The currrently displayed page contains invalid values错误

    现象: eclipse的preferences里面须要保存密码,保存报错Could Not Accept ChangesThe currrently displayed page contains i ...