2017-06-08 04:50:32 +00:00
|
|
|
/**
|
|
|
|
This library was written by Samer Afach, find this code in the repository
|
|
|
|
git.afach.de, 2017
|
|
|
|
|
|
|
|
Terms of use:
|
|
|
|
|
|
|
|
-You're free to do anything you wish with this simple library, including any
|
|
|
|
embedding or modifications you wish, with static or dynamic linking (you can't
|
|
|
|
dynamically link it... it's header only! :-) ).
|
|
|
|
-The creator of this library is not to be help liable for anything you do with
|
|
|
|
it. Using this library is your own responsibility
|
|
|
|
*/
|
|
|
|
|
2017-06-06 17:02:45 +00:00
|
|
|
#ifndef THREADPOOL_H
|
|
|
|
#define THREADPOOL_H
|
|
|
|
|
|
|
|
#include <deque>
|
|
|
|
#include <functional>
|
|
|
|
#include <mutex>
|
|
|
|
#include <thread>
|
|
|
|
#include <vector>
|
2017-06-08 05:34:06 +00:00
|
|
|
#include <condition_variable>
|
2017-06-06 17:02:45 +00:00
|
|
|
|
|
|
|
class ThreadPool {
|
2017-06-06 17:07:50 +00:00
|
|
|
long numOfThreads;
|
2017-06-06 17:02:45 +00:00
|
|
|
std::deque<std::function<void()>> _tasks;
|
|
|
|
std::mutex _queueLock;
|
2017-06-06 17:31:23 +00:00
|
|
|
bool conclude_work = false;
|
|
|
|
bool started_already = false;
|
2017-06-06 17:02:45 +00:00
|
|
|
std::condition_variable _queueCond;
|
|
|
|
std::condition_variable _threadFinishedCond;
|
2017-06-06 17:31:23 +00:00
|
|
|
long num_of_threads_running = 0;
|
2017-06-06 17:02:45 +00:00
|
|
|
std::vector<std::thread> _threads;
|
|
|
|
|
|
|
|
protected:
|
2017-06-06 17:31:23 +00:00
|
|
|
inline void thread_worker();
|
|
|
|
inline void join_all();
|
2017-06-06 17:02:45 +00:00
|
|
|
|
|
|
|
public:
|
|
|
|
inline ThreadPool();
|
|
|
|
inline ~ThreadPool();
|
|
|
|
inline void push(const std::function<void()> &task);
|
|
|
|
inline void
|
2017-06-06 17:07:50 +00:00
|
|
|
start(const long NumOfThreads = std::thread::hardware_concurrency());
|
2017-06-06 17:02:45 +00:00
|
|
|
inline void finish();
|
|
|
|
};
|
|
|
|
|
2017-06-06 17:31:23 +00:00
|
|
|
void ThreadPool::thread_worker() {
|
2017-06-06 17:02:45 +00:00
|
|
|
while (true) {
|
|
|
|
std::unique_lock<decltype(_queueLock)> lg(_queueLock);
|
2017-06-06 17:31:23 +00:00
|
|
|
while (_tasks.empty() && !conclude_work) {
|
2017-06-06 17:02:45 +00:00
|
|
|
_queueCond.wait(lg);
|
|
|
|
}
|
|
|
|
if (!_tasks.empty()) {
|
|
|
|
auto theTask = _tasks[0];
|
|
|
|
_tasks.pop_front();
|
|
|
|
lg.unlock();
|
|
|
|
theTask();
|
|
|
|
lg.lock();
|
|
|
|
}
|
2017-06-06 17:31:23 +00:00
|
|
|
if (_tasks.empty() && conclude_work) {
|
|
|
|
num_of_threads_running--;
|
2017-06-06 17:02:45 +00:00
|
|
|
_threadFinishedCond.notify_one();
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-06-06 17:31:23 +00:00
|
|
|
void ThreadPool::join_all() {
|
2017-06-06 17:02:45 +00:00
|
|
|
for (long i = 0; i < numOfThreads; i++) {
|
|
|
|
if (_threads[i].joinable())
|
|
|
|
_threads[i].join();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
ThreadPool::ThreadPool() {}
|
|
|
|
|
|
|
|
ThreadPool::~ThreadPool() { this->finish(); }
|
|
|
|
|
|
|
|
void ThreadPool::push(const std::function<void()> &task) {
|
|
|
|
std::unique_lock<decltype(_queueLock)> lg(_queueLock);
|
|
|
|
_tasks.push_back(task);
|
|
|
|
_queueCond.notify_one();
|
|
|
|
}
|
|
|
|
|
2017-06-06 17:07:50 +00:00
|
|
|
void ThreadPool::start(const long NumOfThreads) {
|
2017-06-06 17:31:23 +00:00
|
|
|
if (!started_already)
|
|
|
|
started_already = true;
|
2017-06-06 17:02:45 +00:00
|
|
|
else
|
|
|
|
throw std::logic_error("You cannot start the thread pool multiple times.");
|
|
|
|
numOfThreads = NumOfThreads;
|
|
|
|
_threads.reserve(numOfThreads);
|
|
|
|
for (long i = 0; i < numOfThreads; i++) {
|
2017-06-06 17:31:23 +00:00
|
|
|
_threads.push_back(
|
|
|
|
std::thread(std::bind(&ThreadPool::thread_worker, this)));
|
|
|
|
num_of_threads_running++;
|
2017-06-06 17:02:45 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void ThreadPool::finish() {
|
|
|
|
std::unique_lock<decltype(_queueLock)> lg(_queueLock);
|
2017-06-06 17:31:23 +00:00
|
|
|
conclude_work = true;
|
2017-06-06 17:02:45 +00:00
|
|
|
_queueCond.notify_all();
|
2017-06-06 17:31:23 +00:00
|
|
|
while (num_of_threads_running > 0) {
|
2017-06-06 17:02:45 +00:00
|
|
|
_threadFinishedCond.wait(lg);
|
|
|
|
}
|
|
|
|
lg.unlock();
|
2017-06-06 17:31:23 +00:00
|
|
|
join_all();
|
2017-06-06 17:02:45 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
#endif // THREADPOOL_H
|