# mutex跟CRITICAL_SECTION，互斥和临界区

www.MyException.Cn  网友分享于：2013-09-12  浏览：0次
mutex和CRITICAL_SECTION，互斥和临界区

====================================================================================================================================================

https://stackoverflow.com/questions/23519630/are-there-c11-critical-sections

https://stackoverflow.com/questions/9997473/stdmutex-performance-compared-to-win32-critical-section

====================================================================================================================================================

std::mutex慢。CRITICAL_SECTION更快。但是如果采用合理的方式来分割任务，两者可以达到几乎相同的效果。

``` 1 #include <iostream>
2 #include <mutex>
4 #include <Windows.h>
5 #include <chrono>
6
7 using namespace std;
8
9 mutex g_Mutex_Lock, g_Mutex_finish;
10 CRITICAL_SECTION g_CS_Lock, g_CS_finish;
11 uint64_t g_Mutext_Num = -1;
12 uint64_t g_CS_Num = -1;
13 const int32_t g_Count = 10000000;
14 once_flag g_Mutex_flag, g_CS_flag;
15 chrono::time_point<chrono::system_clock> g_Mutex_StartTime, g_CS_StartTime;
16 int32_t g_Mutex_Complete = 0;
17 int32_t g_CS_Complete = 0;
18
19 uint64_t Calculate(uint64_t num, int index)
20 {
21     if (index % 2)
22     {
23         return (num / 0x5555) * 0xaaaa;
24     }
25     else
26     {
27         return (num / 0x6666) * 0x9999;
28     }
29 }
30
31 void mutexTimeStart()
32 {
33     g_Mutex_StartTime = chrono::system_clock::now();
34 }
35
36 void mutexCalculate()
37 {
38     call_once(g_Mutex_flag, mutexTimeStart);
39
40     for (int i = 0; i < g_Count; ++i)
41     {
42         g_Mutex_Lock.lock();
43         g_Mutext_Num = Calculate(g_Mutext_Num, i);
44         g_Mutex_Lock.unlock();
45     }
46     g_Mutex_finish.lock();
47     ++g_Mutex_Complete;
48     if (2 == g_Mutex_Complete)
49     {
50         chrono::duration<double> elapsed_seconds = chrono::system_clock::now() - g_Mutex_StartTime;
51         printf("mutex finished use: %f\n", elapsed_seconds.count());
52     }
53     g_Mutex_finish.unlock();
54 }
55
56 void csTimeStart()
57 {
58     g_CS_StartTime = chrono::system_clock::now();
59 }
60
61 void csCalculate()
62 {
63     call_once(g_CS_flag, csTimeStart);
64     for (int i = 0; i < g_Count; ++i)
65     {
66         EnterCriticalSection(&g_CS_Lock);
67         g_CS_Num = Calculate(g_CS_Num, i);
68         LeaveCriticalSection(&g_CS_Lock);
69     }
70     EnterCriticalSection(&g_CS_finish);
71     ++g_CS_Complete;
72     if (2 == g_CS_Complete)
73     {
74         chrono::duration<double> elapsed_seconds = chrono::system_clock::now() - g_CS_StartTime;
75         printf("cs finished use: %f\n", elapsed_seconds.count());
76     }
77     LeaveCriticalSection(&g_CS_finish);
78 }
79
80
81 void main()
82 {
83     InitializeCriticalSection(&g_CS_Lock);
84     InitializeCriticalSection(&g_CS_finish);
85
87     t3.detach();
89     t4.detach();
90
92     t1.detach();
94     t2.detach();
95
96     int tStop;
97     cin >> tStop;
98 }```
main.cpp

64位release版结果：

1、性能不是瓶颈，不要考虑太多。优化都是在原有的基础上逐步修改改出来的成果，不是动手的时候，脑子就有现成的方案。何况性能并没有走到瓶颈。

2、没有什么比电脑跑出来的结果更靠谱。毕竟电脑才是所有理论知识最终产物的执行者。

3、随时间的推移，技术在改良。使用通用的接口，每次技术更替，你也在享受免费的红利。