-
Notifications
You must be signed in to change notification settings - Fork 9
Expand file tree
/
Copy pathfind.best.cpp
More file actions
44 lines (34 loc) · 865 Bytes
/
find.best.cpp
File metadata and controls
44 lines (34 loc) · 865 Bytes
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
#include <iostream>
#include <thread>
#include <atomic>
#include <random>
#include <array>
using namespace std;
void update_cur_best(atomic<int>& best, int a, int b)
{
if (a < b)
a = b;
auto cur_best = best.load(memory_order_relaxed);
while (cur_best < a && !best.compare_exchange_weak(cur_best, a))
;
}
void run(int max, std::atomic<int>& best)
{
mt19937 generator{random_device{}()};
uniform_int_distribution<int> distribution{0, max};
for (int i = 0; i < 15; ++i)
update_cur_best(best, distribution(generator), distribution(generator));
}
// g++-9 -std=c++2a ./find.best.cpp
int main()
{
atomic<int> best{0};
const int max = 100;
array<thread, 3> threads;
for (auto& t : threads)
t = thread(run, max, sref(best));
for (auto& t : threads)
t.join();
cout << "best = " << best << endl;
return 0;
}