C++0X(C++11)的std:mutex、std::thread与Windows API性能对比

来源:互联网 发布:风险模拟软件 编辑:程序博客网 时间:2024/06/05 09:40

转载处:http://aigo.iteye.com/blog/1908084

声明:请看到这篇文章的人务必去阅读原文,我转载的目的仅为了学习

C++0X标准提供的std::mutex和std::thread两个接口开发多线程同步的应用非常方便,而且可以跨平台,自己做了一下测试,发现这个跨平台的代价还是很大的,我分别用std::mutex与Windows的CRITICAL_SECTION、std::thead和WIndows的CreateThread接口做了对比,测试代码如下:

Cpp代码  收藏代码
  1. #include "stdafx.h"  
  2. #include <mutex>  
  3. #include <atomic>   
  4. #include <iostream>  
  5. #include <time.h>  
  6. #include <thread>  
  7. #include <list>  
  8. #include <atomic>   
  9. #include <Windows.h>  
  10.   
  11. using namespace std;  
  12.   
  13. #define MAX_THREADS 16  
  14.   
  15. // 全局的结果数据   
  16. long total1 = 0;   
  17. long total2 = 0;  
  18. std::atomic<long> total;  
  19.   
  20. std::mutex m_lock;  
  21.   
  22. CRITICAL_SECTION m_Lock2;  
  23.   
  24. void use_std_mutex();  
  25. void use_win_critical();  
  26. void use_win_thread();  
  27.   
  28.   
  29. void test_mutex()  
  30. {  
  31.     for(int i=0; i<1000000;++i)  
  32.     {  
  33.         m_lock.lock();  
  34.         total1 += 1;       
  35.         m_lock.unlock();  
  36.     }  
  37. }  
  38.   
  39. void test_critical()  
  40. {  
  41.     for(int i=0; i<1000000;++i)  
  42.     {  
  43.         EnterCriticalSection(&m_Lock2);  
  44.         total2 += 1;       
  45.         LeaveCriticalSection(&m_Lock2);  
  46.     }  
  47. }  
  48.   
  49. int main(int argc, char* argv[])  
  50. {  
  51.     use_std_mutex();  
  52.     use_win_critical();  
  53.     use_win_thread();  
  54.       
  55.     return 0;  
  56. }  
  57.   
  58. void use_std_mutex()  
  59. {  
  60.     std::list<std::thread*> threadlist;  
  61.   
  62.     //测试mutex  
  63.     printf("testing mutex...\n");  
  64.     clock_t start = clock();  
  65.     for(int i=0; i<MAX_THREADS; ++i)   
  66.     {  
  67.         std::thread *t1 = new std::thread((&test_mutex));  
  68.         threadlist.push_back(t1);  
  69.     }  
  70.     for(std::list<std::thread*>::const_iterator i = threadlist.begin(); i != threadlist.end(); i++ )  
  71.     {  
  72.         (*i)->join();  
  73.     }  
  74.     clock_t finish = clock();  
  75.     printf("result:%d\n", total1);  
  76.     printf("cost:%dms\n", finish - start);  
  77.     for(std::list<std::thread*>::const_iterator i = threadlist.begin(); i != threadlist.end(); i++ )  
  78.     {  
  79.         delete(*i);  
  80.     }  
  81. }  
  82.   
  83. void use_win_critical()  
  84. {  
  85.     //测试Critical  
  86.     InitializeCriticalSection(&m_Lock2);  
  87.     std::list<std::thread*> threadlist;  
  88.     printf("testing critical...\n");  
  89.     clock_t start = clock();  
  90.     for(int i=0; i<MAX_THREADS; ++i)   
  91.     {  
  92.         std::thread *t1 = new std::thread((&test_critical));  
  93.         threadlist.push_back(t1);  
  94.     }  
  95.     for(std::list<std::thread*>::const_iterator i = threadlist.begin(); i != threadlist.end(); i++ )  
  96.     {  
  97.         (*i)->join();  
  98.     }  
  99.     clock_t finish = clock();  
  100.     printf("result:%d\n", total2);  
  101.     printf("cost:%dms\n", finish - start);  
  102.     for(std::list<std::thread*>::const_iterator i = threadlist.begin(); i != threadlist.end(); i++ )  
  103.     {  
  104.         delete(*i);  
  105.     }  
  106. }  
  107.   
  108.   
  109. #define BUF_SIZE 255  
  110. long total3 = 0;   
  111. CRITICAL_SECTION m_Lock3;  
  112.   
  113. DWORD WINAPI MyThreadFunction( LPVOID lpParam );  
  114. //使用Windows线程测试  
  115. void use_win_thread()  
  116. {  
  117.     DWORD   dwThreadIdArray[MAX_THREADS];  
  118.     HANDLE  hThreadArray[MAX_THREADS];   
  119.   
  120.     InitializeCriticalSection(&m_Lock3);  
  121.     printf("testing use_win_thread...\n");  
  122.   
  123.     clock_t start = clock();  
  124.     forint i=0; i<MAX_THREADS; i++ )  
  125.     {  
  126.         hThreadArray[i] = CreateThread(   
  127.             NULL,                     
  128.             0,                        
  129.             MyThreadFunction,         
  130.             NULL,                     
  131.             0,                        
  132.             &dwThreadIdArray[i]);     
  133.   
  134.     }   
  135.   
  136.     WaitForMultipleObjects(MAX_THREADS, hThreadArray, TRUE, INFINITE);  
  137.   
  138.     clock_t finish = clock();  
  139.     printf("result:%d\n", total3);  
  140.     printf("cost:%dms\n", finish - start);  
  141.   
  142.     for(int i=0; i<MAX_THREADS; i++)  
  143.     {  
  144.         CloseHandle(hThreadArray[i]);  
  145.     }  
  146.   
  147. }  
  148.   
  149. DWORD WINAPI MyThreadFunction( LPVOID lpParam )   
  150. {   
  151.     for(int i=0; i<1000000;++i)  
  152.     {  
  153.         EnterCriticalSection(&m_Lock3);  
  154.         total3 += 1;       
  155.         LeaveCriticalSection(&m_Lock3);  
  156.     }  
  157.     return 0;   
  158. }   

测试环境:
硬件:i7 2630qm 4核

系统:Windows7 64bit 旗舰版  SP1

程序:VS2012 Release win32

 

测试结果:

2线程抢占:

testing mutex...                    ----  result:2000000  ----  cost:628ms

testing critical...                   ----  result:2000000  ----  cost:132ms

testing use_win_thread...   ----  result:2000000  ----  cost:98ms


4线程抢占:

testing mutex...                    ----  result:4000000  ----  cost:1150ms

testing critical...                   ----  result:4000000  ----  cost:266ms

testing use_win_thread...   ----  result:4000000  ----  cost:216ms

 

8线程抢占:

testing mutex...                    ----  result:8000000  ----  cost:2855ms

testing critical...                   ----  result:8000000  ----  cost:582ms

testing use_win_thread...   ----  result:8000000  ----  cost:461ms

 

16线程抢占:

testing mutex...                    ----  result:16000000 ----  cost:138052ms

testing critical...                   ----  result:16000000 ----  cost:1448ms

testing use_win_thread...   ----  result:16000000 ----  cost:1169ms

 

结论:如果想追求高性能,C++11的std::mutex不要用,std::thread性能损耗不大,要用随意。


0 0
原创粉丝点击