-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathpsbasic.cpp
More file actions
90 lines (75 loc) · 2.22 KB
/
Copy pathpsbasic.cpp
File metadata and controls
90 lines (75 loc) · 2.22 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
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
#include <bits/stdc++.h>
using namespace std;
// ✅ Custom Comparator for Priority Queue
struct CompareTask {
bool operator()(const pair<int, function<void()>>& a, const pair<int, function<void()>>& b) {
return a.first > b.first;
}
};
class Executor {
priority_queue<pair<int, function<void()>>, vector<pair<int, function<void()>>>, CompareTask> taskQueue;
vector<future<void>> futures;
mutex queueMutex;
condition_variable cv;
atomic<bool> isShutdown{false};
atomic<int> activeTasks{0};
const int MAX_CONCURRENT_TASKS = 8;
public:
Executor() {}
void addTask2Queue(function<void()> task, int priority = 10) {
{
lock_guard<mutex> lock(queueMutex);
taskQueue.push({priority, task});
}
cv.notify_one();
executeTasksAsync();
}
void executeTasksAsync() {
while (true) {
function<void()> task;
{
unique_lock<mutex> lock(queueMutex);
if (taskQueue.empty() || isShutdown || activeTasks >= MAX_CONCURRENT_TASKS) return;
task = taskQueue.top().second;
taskQueue.pop();
activeTasks++;
}
// ✅ Run asynchronously
futures.push_back(std::async(launch::async, [this, task]() {
task();
activeTasks--;
}));
}
}
void shutdown() {
if (isShutdown) return;
cout << "Shutdown called" << endl;
{
lock_guard<mutex> lock(queueMutex);
isShutdown = true;
}
cv.notify_all();
// ✅ Ensure all tasks are completed
for (auto& fut : futures) {
if (fut.valid()) {
fut.get();
}
}
}
~Executor() {
shutdown();
}
};
int main() {
Executor obj;
cout << "Object created" << endl;
for (int i = 0; i < 100000000000; i++) {
obj.addTask2Queue([i] {
cout << "[Task] " << i << " executed by " << this_thread::get_id() << endl;
this_thread::sleep_for(chrono::seconds(2));
}, rand() % 10);
}
this_thread::sleep_for(chrono::seconds(5));
obj.shutdown();
return 0;
}