-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathTasks.h
83 lines (66 loc) · 1.71 KB
/
Tasks.h
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
#pragma once
#include <chrono>
#include <cstdlib>
#include <future>
#include <iostream>
#include <map>
#include <mutex>
#include <set>
#include <thread>
#include <utility>
#include <vector>
// TODO: supply the work to do in 'doWork' as a lambda
class Task;
class TaskManager {
public:
TaskManager(const std::vector<Task*>& tasks) : tasks_(tasks) {}
bool doTasks()
{
std::cout << "\n\ncheckTasksForCycle:\n";
if (checkTasksForCycle()) {
std::cout << "YES Found a cycle, tasks not executed\n";
return false;
}
else
{
std::cout << "NO Cycle, executing tasks...\n";
executeTasks();
}
std::cout << "Done\n";
return true;
}
bool isFinished(int index) {
return finished_[index];
}
void finish(int index) {
finished_[index] = true;
}
bool checkTasksForCycle();
std::mutex mu_finished_;
protected:
void executeTasks();
private:
std::vector<Task*> tasks_;
std::map<int, bool> finished_;
}; // TaskManager
class Task {
public:
Task(int index, std::set<int> dependencies)
: index_(index), dependencies_(dependencies) {}
// Task is non-movable and non-copyable
Task(const Task&) = delete;
Task(Task&&) = delete;
Task& operator=(const Task&) = delete;
Task& operator=(Task&&) = delete;
void run(TaskManager*);
std::promise<int> promise_;
const int index_;
const std::set<int> dependencies_;
std::vector <std::shared_future<int>> dependancyResults_;
void setWork(std::function<void(const std::vector <int>& f)> doWork_);
private:
std::function<void(const std::vector <int>&)> doWork_ = [](const std::vector <int> &) {
const auto end_time = std::chrono::steady_clock::now() + std::chrono::milliseconds(100);
while (std::chrono::steady_clock::now() < end_time) {}
};
}; // Task