-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy paththreads.cpp
133 lines (115 loc) · 3.5 KB
/
threads.cpp
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
#include <atomic>
#include <iostream>
#include <map>
#include <mutex>
#include <thread>
long counter1 = 0;
void increment_counter(long n) {
for (long i = 0; i < n; i++) counter1 += 1;
};
void race_condition() {
std::cout << "Counter before creating threads " << counter1 << "\n";
std::thread t1(increment_counter, 1000000);
std::thread t2(increment_counter, 1000000);
std::cout << "Counter just after creating " << counter1 << "\n";
t1.join();
t2.join();
std::cout << "Counter after join " << counter1 << "\n";
}
std::atomic<long> counter2{0};
void atomic_increment_counter(long n) {
for (long i = 0; i < n; i++) counter2.fetch_add(1);
};
void atomic_increment() {
std::cout << "Counter before creating threads " << counter2 << "\n";
std::thread t1(atomic_increment_counter, 1000000);
std::thread t2(atomic_increment_counter, 1000000);
std::cout << "Counter just after creating " << counter2 << "\n";
t1.join();
t2.join();
std::cout << "Counter after join " << counter2 << "\n";
}
long counter3 = 0;
std::mutex counter_mutex;
void increment_with_mutex(long n) {
for (long i = 0; i < n; i++) {
counter_mutex.lock();
counter3 += 1;
counter_mutex.unlock();
}
};
void mutex_increment() {
std::cout << "Counter before creating threads " << counter3 << "\n";
std::thread t1(increment_with_mutex, 1000000);
std::thread t2(increment_with_mutex, 1000000);
std::cout << "Counter just after creating " << counter3 << "\n";
t1.join();
t2.join();
std::cout << "Counter after join " << counter3 << "\n";
}
long counter4 = 0;
void increment_with_guard(long n) {
for (long i = 0; i < n; i++) {
std::lock_guard<std::mutex> lock(counter_mutex);
counter4 += 1;
}
};
void guard_increment() {
std::cout << "Counter before creating threads " << counter4 << "\n";
std::thread t1(increment_with_guard, 1000000);
std::thread t2(increment_with_guard, 1000000);
std::cout << "Counter just after creating " << counter4 << "\n";
t1.join();
t2.join();
std::cout << "Counter after join " << counter4 << "\n";
}
class CounterHolder {
private:
std::atomic<long> counter_{0};
public:
void increment(long n) {
for (long i = 0; i < n; i++) counter_.fetch_add(1);
}
long get() { return counter_.load(); };
~CounterHolder() { std::cout << "calling dtor\n"; };
};
void class_method() {
auto counter = std::make_shared<CounterHolder>();
std::cout << "Counter before creating threads " << counter->get() << "\n";
std::thread t1(&CounterHolder::increment, counter, 1000000);
std::thread t2(&CounterHolder::increment, counter, 1000000);
std::cout << "Counter just after creating " << counter->get() << "\n";
t1.join();
t2.join();
std::cout << "Counter after join " << counter->get() << "\n";
}
class MapHolder {
private:
std::map<int, std::string> map_;
std::mutex mutex_;
public:
void put(std::string value, long n) {
for (long i = 0; i < n; i++) {
std::lock_guard<std::mutex> lock(mutex_);
map_[i] = value;
}
}
std::map<int, std::string> get() { return map_; };
~MapHolder() { std::cout << "calling dtor\n"; };
};
void map_concurrent_access() {
auto map = std::make_shared<MapHolder>();
std::thread t1(&MapHolder::put, map, "abc", 1000000);
std::thread t2(&MapHolder::put, map, "def", 1000000);
t1.join();
t2.join();
std::cout << "Map size after join " << map->get().size() << "\n";
}
int main() {
// race_condition();
// atomic_increment();
// mutex_increment();
// guard_increment();
// class_method();
map_concurrent_access();
}