1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
// MutexTest.cpp: 콘솔 응용 프로그램의 진입점을 정의합니다.
//
 
#include "stdafx.h"
 
#include <iostream>
#include <Windows.h>
#include <thread>
#include <mutex>
 
#include <time.h>
 
int g_index = 1000;
 
int operateCount = 0;
 
#include <list>
 
 
static std::recursive_mutex rMutex;
 
void Increase(HANDLE hMutex) {
    std::unique_lock<std::recursive_mutex> lock(rMutex, std::defer_lock); // 초기화 시간을 제외하기 위해
 
    for (int i = 0; i < 100000; i++) {
        WaitForSingleObject(hMutex, INFINITE);
        //g_index++;
 
        operateCount++;
        
        
        ReleaseMutex(hMutex);
    }
 
}
 
void Decrease(HANDLE hMutex) {
    std::unique_lock<std::recursive_mutex> lock(rMutex, std::defer_lock); // 초기화 시간을 제외하기 위해
 
    for (int i = 0; i < 100000; i++) {
        WaitForSingleObject(hMutex, INFINITE);
        
        operateCount++;
        
 
        ReleaseMutex(hMutex);
    }
}
 
 
//static std::recursive_mutex rMutex;
 
//std::unique_lock<std::recursive_mutex> lock(rMutex, std::defer_lock);
 
void Increase2() {
    std::unique_lock<std::recursive_mutex> lock(rMutex, std::defer_lock);
 
    for (int i = 0; i < 100000; i++){
 
        lock.lock();
        operateCount++;
 
 
        lock.unlock();
    }
 
}
 
void Decrease2() {
 
    std::unique_lock<std::recursive_mutex> lock(rMutex, std::defer_lock);
    for (int i = 0; i < 100000; i++) {
        lock.lock();
        operateCount++;
        lock.unlock();
    }
 
}
 
 
 
static std::chrono::system_clock::time_point start;
 
int main()
{
 
    HANDLE hMutex = CreateMutex(NULL, FALSE, NULL);
    
    //std::unique_lock<std::recursive_mutex> lock(rMutex, std::defer_lock);
 
    //매개 변수로 넘기려고 했으나,, 복제가 안된다고 한다
 
    /*
        CreateMutex VS unique_lock을 통한 recursiveMutex의
        시간 차이를 비교해 본결 과,
        전자는 대략 1초
        후자는 대략 0.08초가 걸렸다.
        즉 시간차이는 10배 이상 차이가 난다.
        전자는 좀 더 세밀한 동기화를 위해 필요하고
        후자는 세밀한 동기화가 필요없고 원자성만 보장하고 싶을 때
        사용하는 것이 적합할 것 같다.
    
    */
    
    start = std::chrono::system_clock::now();
 
//    std::thread t1(Increase, hMutex);
//    std::thread t2(Decrease, hMutex);
 
 
    std::thread t1(Increase2);
    std::thread t2(Decrease2);
 
    t1.join();
    t2.join();
 
    std::chrono::duration<double> sec = std::chrono::system_clock::now() - start;
 
    std::cout << "수행 시간 : " << sec.count() << std::endl;
    //std::chrono::seconds duration(1);
    //std::this_thread::sleep_for(duration);
    
    printf("operate Count : %d\n", operateCount);
    
    getchar();
    return 0;
}
 
 
cs



recursive_mutex 와 winapi를 통해 만드는 mutex와  둘 중에 뭘 쓸까 고민하다가


비교를 해보기로 했다.


결과는 코드에 써있다.  


CreateMutex VS unique_lock을 통한 recursiveMutex의


시간 차이를 비교해 본결 과,


전자는 대략 1초


후자는 대략 0.08초가 걸렸다.


즉 시간차이는 10배 이상 차이가 난다.


전자는 좀 더 세밀한 동기화를 위해 필요하고


후자는 세밀한 동기화가 필요없고 원자성만 보장하고 싶을 때


사용하는 것이 적합할 것 같다.




+ Recent posts