-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathThreadPoolSessions.cpp
More file actions
56 lines (50 loc) · 1.72 KB
/
ThreadPoolSessions.cpp
File metadata and controls
56 lines (50 loc) · 1.72 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
/*
* Copyright (C) 2021 Ilya Entin
*/
/*
This thread pool works with any number of different
runnable types with constraints on the number of
running objects of every type and on the total number
of running objects. It creates threads on demand
not creating redundant threads.
*/
#include "ThreadPoolSessions.h"
ThreadPoolSessions::ThreadPoolSessions(int maxSize) :
ThreadPoolBase(maxSize) {}
void ThreadPoolSessions::calculateStatus(RunnablePtr runnable) {
++_totalNumberObjects;
// need one more thread ?
bool condition1 = _totalNumberObjects > size();
// can run one more of type ?
bool condition2 = runnable->getNumberRunningByType() < runnable->_maxNumberRunningByType;
if (condition1 && condition2 && size() < _maxSize) {
createThread();
Debug << "numberOfThreads=" << size() << ' ' << runnable->getType() << '\n';
}
else if (!condition2)
runnable->_status = STATUS::MAX_OBJECTS_OF_TYPE;
else if (runnable->_numberRunningTotal >= _maxSize)
runnable->_status = STATUS::MAX_TOTAL_OBJECTS;
runnable->displayCapacityCheck(_totalNumberObjects);
}
STATUS ThreadPoolSessions::push(RunnablePtr runnable) {
std::lock_guard lock(_queueMutex);
_queue.emplace_back(runnable);
_queueCondition.notify_one();
return runnable->_status;
}
RunnablePtr ThreadPoolSessions::get() {
std::unique_lock lock(_queueMutex);
_queueCondition.wait(lock, [this] { return !_queue.empty() || _stopped; });
if (_stopped)
return RunnablePtr();
for (auto it = _queue.begin(); it != _queue.end(); ++it) {
RunnablePtr runnable = *it;
if (runnable->getNumberRunningByType() < runnable->_maxNumberRunningByType &&
!runnable->_stopped) {
_queue.erase(it);
return runnable;
}
}
return RunnablePtr();
}