-
Notifications
You must be signed in to change notification settings - Fork 68
/
fiboMT.cpp
52 lines (46 loc) · 1.16 KB
/
fiboMT.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
#include <iostream>
#include <cstring>
#include <sstream>
#include <thread>
#include <chrono>
#include <random>
constexpr auto NBITERATIONS = 5;
constexpr auto MIN = 22;
constexpr auto MAX = 25;
struct WorkToDo {
char* title;
int a;
};
unsigned int fibo(unsigned a) {
if (a == 1 || a == 0) {
return 1;
} else {
return fibo(a-1)+fibo(a-2);
}
}
void computation(WorkToDo* work, unsigned long* result) {
*result = fibo(work->a);
std::free(work->title);
work->title = nullptr;
}
void launchFibo(const char* title, int a) {
WorkToDo w;
w.title = strdup(title);
w.a = a;
unsigned long result;
std::thread t{computation, &w, &result};
std::this_thread::sleep_for(std::chrono::microseconds{1});
std::cout << "Computing " << w.title << '\n';
t.join();
std::cout << title << " = " << (unsigned long)result << '\n';
}
int main() {
std::default_random_engine e;
std::uniform_int_distribution d{MIN, MAX};
for (unsigned int i = 0; i < NBITERATIONS; i++) {
unsigned int a = d(e);
std::stringstream ss;
ss << "Fibo(" << a << ")";
launchFibo(ss.str().c_str(), a);
}
}