-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathThreadCPP.cpp
More file actions
63 lines (53 loc) · 1.62 KB
/
Copy pathThreadCPP.cpp
File metadata and controls
63 lines (53 loc) · 1.62 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
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
#include <Thread.h>
#include <Prime.h>
#include <iostream>
#include <chrono>
#include <sstream>
using namespace std;
using namespace std::chrono;
int main(int argc, char** argv) {
int n = stoi(argv[1]);
/*
try {
n = stoi(argv[1]);
} catch (exception& e) {
cout << "Invalid argument" << endl;
return 1;
}*/
class constructor : public Runnable {
private:
int num;
time_point<system_clock> startAt, endAt;
double threadId() {
stringstream ss;
ss << this_thread::get_id();
return stod(ss.str());
};
public:
constructor(int n) {
this->num = n;
this->startAt = system_clock::now();
};
virtual void run() override {
Prime* prime = new Prime(this->num);
int count = prime->count();
duration<double> endAt = system_clock::now() - this->startAt;
printf("C++ thread id #%.f: executes %d prime numbers in %.f ms\n",
threadId(), count, endAt.count() * 1000);
}
} runnable (n);
// Thread t0 = Thread(&runnable);
// Thread t1 = Thread(&runnable);
// Thread t2 = Thread(&runnable);
// Thread t3 = Thread(&runnable);
cout << "C++ Threads - initializing complete" << endl;
thread t0(&Runnable::run, &runnable);
thread t1(&Runnable::run, &runnable);
thread t2(&Runnable::run, &runnable);
thread t3(&Runnable::run, &runnable);
t0.join();
t1.join();
t2.join();
t3.join();
return 0;
}