-
Notifications
You must be signed in to change notification settings - Fork 2
/
main.cpp
63 lines (53 loc) · 1.62 KB
/
main.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
#include <iostream>
#include "JobScheduler.h"
#define NUMBER_OF_TASKS 8
#define FAST_TAG 1
#define SLOW_TAG 2
using namespace std;
/* Example */
class HelloWorldJob : public Job {
int taskid;
public:
HelloWorldJob(int id) : Job(), taskid(id) {}
void run() override {
printf("Hello World from task %d\n", taskid);
}
};
class SlowJob : public Job {
int taskid;
public:
SlowJob(int id, int tag = NOTAG) : Job(tag), taskid(id) {}
void run() override {
for (int i = 0 ; i >= 0 ; i++); // waste quite some time
printf("Slow job with task id %d finished!\n", taskid);
}
};
class FastJob : public Job {
int taskid;
public:
FastJob(int id, int tag = NOTAG) : Job(tag), taskid(id) {}
void run() override {
for (int i = 0 ; i < 1000 ; i++); // waste less time
printf("Fast job with task id %d finished!\n", taskid);
}
};
int main(){
JobScheduler scheduler; // use default number of threads
for (int i = 0 ; i < NUMBER_OF_TASKS ; i++){
scheduler.schedule(new HelloWorldJob(i)); // schedule new jobs
}
scheduler.waitUntilJobsHaveFinished(); // block (if needed) until all jobs have finished
/////////////////////////////////////////////
for (int i = 0 ; i < NUMBER_OF_THREADS ; i++){
if (i % 2 == 0){
scheduler.schedule(new SlowJob(i, SLOW_TAG)); // schedule new slow job
} else {
scheduler.schedule(new FastJob(i, FAST_TAG)); // schedule new fast job
}
}
scheduler.waitUntilJobsHaveFinished(FAST_TAG);
printf("Waited for FAST jobs!\n");
scheduler.waitUntilJobsHaveFinished();
printf("Waited for all remaining jobs!\n");
return 0;
}