-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathexample_run.cpp
103 lines (98 loc) · 2.29 KB
/
example_run.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
#include <unistd.h>
#include <math.h>
#include <atomic>
#include <mutex>
#include <thread>
#include <vector>
#include <iostream>
#include <vector>
#include <cyclicbarrier.hpp>
class prime_checker : public cbar::callable {
public:
prime_checker(uint32_t waitfor) {
this->waitfor = waitfor;
}
virtual ~prime_checker(){
}
virtual void run() override {
std::cout << "Computation started" << std::endl;
}
void add_prime(uint32_t prime) {
std::lock_guard<std::mutex> lck(lock);
primes.push_back(prime);
}
static bool is_prime(uint32_t number) {
if (number <= 1 ) {
return false;
}
if (number <= 3) {
return true;
}
auto end = (uint32_t)sqrt(number);
uint32_t i = 2;
while (i <= end) {
if (number % i == 0) {
return false;
}
if (i == 2) {
i++;
} else {
i += 2;
}
}
return true;
}
void iam_done() {
std::lock_guard<std::mutex> lck(lock);
completed++;
if (completed != waitfor) {
return;
}
auto it = primes.begin();
while (it != primes.end()){
std::cout << *it << " is prime " << std::endl;
++it;
}
}
private:
std::mutex lock;
std::vector<int> primes;
uint32_t waitfor = 10;
uint32_t completed = 0;
};
void primecompute(cbar::cyclicbarrier *cb, prime_checker *pc, uint32_t end,
std::atomic<uint32_t>* start) {
cb->await(100000000000);
while (true) {
auto cur = start->fetch_add(1);
if (cur > end) break;
if (pc->is_prime(cur)){
pc->add_prime(cur);
}
}
pc->iam_done();
return;
}
int main() {
auto pc = new prime_checker(10);
auto c = new cbar::cyclicbarrier(10, pc);
auto start = new std::atomic<uint32_t>();
int i = 0;
std::vector<std::thread*> ts;
while (i++ < 10) {
std::thread *t = new std::thread(primecompute, c, pc, 10000, start);
ts.push_back(t);
}
i = 0;
while (i < 10) {
ts[i++]->join();
}
i = 0;
while (i < 10) {
delete ts[i++];
}
delete pc;
delete c;
delete start;
return 0;
}