-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathThreadPoolDeq.h
More file actions
129 lines (102 loc) · 2.74 KB
/
Copy pathThreadPoolDeq.h
File metadata and controls
129 lines (102 loc) · 2.74 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
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
#pragma once
#include "TSDeque.h"
#include <atomic>
#include <functional>
#include <iostream>
#include <memory>
#include <random>
#include <thread>
#include <vector>
// Instead of std::function<void()>
typedef void (*TaskFunc)(void *);
struct Task {
TaskFunc func;
void *arg;
void operator()() const noexcept {
if (func)
func(arg);
}
};
template <typename T> class ThreadPoolDeq {
private:
std::unique_ptr<TSDeque<T>[]> dequeList;
std::vector<std::jthread> workers;
std::mutex rand_mut;
std::mt19937 mt;
std::atomic<int> pos{0};
int maxThreads{2};
void worker_loop(int ind) {
while (true) {
T task;
bool taskFound = false;
// phase 1: trying non blocking in our own queue
if (dequeList[ind].tryPopAtFront(task)) {
taskFound = true;
}
// phase 2: trying non blocking by stealing from another queue
if (!taskFound) {
TryStealing(taskFound, ind, task);
}
if (taskFound) {
task();
} else {
if (dequeList[ind].popAtFront(task)) {
// now since we did not get any task directly
// we'll try a blocking way to get task
task();
} else {
// we now want to close the thread hence break
break;
}
}
}
}
void TryStealing(bool &taskFound, int ind, T &task) {
// attempting to get a task from n different threads
int randStart = get_random();
for (int i = 0; i < maxThreads; i++) {
int checkInd = (randStart + i) % maxThreads;
if (dequeList[checkInd].tryPopAtBack(task)) {
taskFound = true;
break;
}
}
}
int get_random() {
std::lock_guard<std::mutex> lck(rand_mut);
std::uniform_int_distribution<int> dist(0, maxThreads - 1);
return dist(mt);
}
public:
ThreadPoolDeq() : maxThreads(std::thread::hardware_concurrency()) {
dequeList = std::make_unique<TSDeque<T>[]>(maxThreads);
for (int i = 0; i < maxThreads; i++) {
workers.emplace_back(&ThreadPoolDeq::worker_loop, this, i);
}
}
void addTask(std::function<void()> task) {
if (task == nullptr)
return;
int currentPos = pos.load();
int nextPos;
do {
nextPos = (currentPos + 1) % maxThreads;
} while (!pos.compare_exchange_strong(currentPos, nextPos));
// dequeList[currentPos].insertAtFront(std::move(task));
dequeList[currentPos].insertAtFront(std::move(task));
}
void submitTask(Task& task) {
// if (task == nullptr)
// return;
int i = 0;
do {
i = get_random();
} while (!dequeList[i].tryInsertAtFront(task));
}
~ThreadPoolDeq() {
for (int i = 0; i < maxThreads; i++) {
dequeList[i].close();
}
// now the jthreads will close/ join themselves
}
};