/
175_test_atomic.cpp
61 lines (47 loc) · 1.51 KB
/
175_test_atomic.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
/*================================================================
* Copyright (C) 2019 Renleilei. All rights reserved.
*
* 文件名称:175_test_atomic.cpp
* 创 建 者:Renleilei (renleilei1992@foxmail.com)
* 创建日期:2019年08月21日
* 描 述:
* 版 本: Version 1.00
================================================================*/
#include <iostream> // std::cout, std::endl
#include <atomic> // std::atomic, std::atomic_flag, ATOMIC_FLAG_INIT
#include <vector> // std::vector
#include <thread> // std::thread
using namespace std;
// 初始化一个 std::atomic<bool> 类型的原子变量
std::atomic<bool> ready(false);
std::atomic_flag winner = ATOMIC_FLAG_INIT; //
void do_count1m(int id);
int main()
{
std::vector<std::thread> vec_threads;
cout << "spawning 10 threads that count to 1 millon..." << endl;
for (int i = 0; i < 10; ++i) {
vec_threads.push_back(std::thread(do_count1m, i));
}
ready = true;
for (auto & th : vec_threads) {
if (th.joinable()) {
th.join();
}
}
return 0;
}
void do_count1m(int id)
{
while (!ready) {
std::this_thread::yield(); // 等待ready变为true
}
cout << __func__ << " ready is true!!" << endl;
for (volatile int i = 0; i < 1000000; ++i) {
// 计数
}
// 最先执行的线程将会设置此winner flag并返回false;
if (!winner.test_and_set()) { // 如果atomic_flag对象被设置,则返回true; 如果atomic_flag对象未被设置,则设置之,返回false
cout << "thread #" << id << " won!!" << endl;
}
}