-
Notifications
You must be signed in to change notification settings - Fork 11
Expand file tree
/
Copy pathatomic1.cpp
More file actions
47 lines (40 loc) · 1.39 KB
/
atomic1.cpp
File metadata and controls
47 lines (40 loc) · 1.39 KB
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
#include <chrono>
#include <atomic>
#include <thread>
#include <iostream>
std::chrono::milliseconds interval(100);
std::atomic<bool> readyFlag(false); //原子布尔类型,取代互斥量
std::atomic<int> job_shared(0); //两个线程都能修改'job_shared',将该变量特化为原子类型
int job_exclusive = 0; //只有一个线程能修改'job_exclusive',不需要保护
//此线程只能修改 'job_shared'
void job_1()
{
std::this_thread::sleep_for(5 * interval);
job_shared.fetch_add(1);
std::cout << "job_1 shared (" << job_shared.load() << ")\n";
readyFlag.store(true); //改变布尔标记状态为真
}
// 此线程能修改'job_shared'和'job_exclusive'
void job_2()
{
while (true) { //无限循环,直到可访问并修改'job_shared'
if (readyFlag.load()) { //判断布尔标记状态是否为真,为真则修改‘job_shared’
job_shared.fetch_add(1);
std::cout << "job_2 shared (" << job_shared.load() << ")\n";
return;
} else { //布尔标记为假,则修改'job_exclusive'
++job_exclusive;
std::cout << "job_2 exclusive (" << job_exclusive << ")\n";
std::this_thread::sleep_for(interval);
}
}
}
int main()
{
std::thread thread_1(job_1);
std::thread thread_2(job_2);
thread_1.join();
thread_2.join();
getchar();
return 0;
}