Added basic C++11 thread pool
This commit is contained in:
parent
96870602d5
commit
082033d384
1 changed files with 111 additions and 0 deletions
111
base/threadpool.hpp
Normal file
111
base/threadpool.hpp
Normal file
|
|
@ -0,0 +1,111 @@
|
||||||
|
/*
|
||||||
|
* Basic C++11 based thread pool with per-thread job queues
|
||||||
|
*
|
||||||
|
* Copyright (C) 2016 by Sascha Willems - www.saschawillems.de
|
||||||
|
*
|
||||||
|
* This code is licensed under the MIT license (MIT) (http://opensource.org/licenses/MIT)
|
||||||
|
*/
|
||||||
|
|
||||||
|
#include <vector>
|
||||||
|
#include <thread>
|
||||||
|
#include <queue>
|
||||||
|
#include <mutex>
|
||||||
|
|
||||||
|
namespace vkTools
|
||||||
|
{
|
||||||
|
class Thread
|
||||||
|
{
|
||||||
|
private:
|
||||||
|
bool destroying = false;
|
||||||
|
std::thread worker;
|
||||||
|
std::queue<std::function<void()>> jobQueue;
|
||||||
|
std::mutex queueMutex;
|
||||||
|
std::condition_variable condition;
|
||||||
|
|
||||||
|
// Loop through all remaining jobs
|
||||||
|
void queueLoop()
|
||||||
|
{
|
||||||
|
while (true)
|
||||||
|
{
|
||||||
|
std::function<void()> job;
|
||||||
|
{
|
||||||
|
std::unique_lock<std::mutex> lock(queueMutex);
|
||||||
|
condition.wait(lock, [this] { return !jobQueue.empty() || destroying; });
|
||||||
|
if (destroying)
|
||||||
|
{
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
job = jobQueue.front();
|
||||||
|
}
|
||||||
|
|
||||||
|
job();
|
||||||
|
|
||||||
|
{
|
||||||
|
std::lock_guard<std::mutex> lock(queueMutex);
|
||||||
|
jobQueue.pop();
|
||||||
|
condition.notify_one();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
public:
|
||||||
|
Thread()
|
||||||
|
{
|
||||||
|
worker = std::thread(&Thread::queueLoop, this);
|
||||||
|
}
|
||||||
|
|
||||||
|
~Thread()
|
||||||
|
{
|
||||||
|
if (worker.joinable())
|
||||||
|
{
|
||||||
|
wait();
|
||||||
|
queueMutex.lock();
|
||||||
|
destroying = true;
|
||||||
|
condition.notify_one();
|
||||||
|
queueMutex.unlock();
|
||||||
|
worker.join();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// Add a new job to the thread's queue
|
||||||
|
void addJob(std::function<void()> function)
|
||||||
|
{
|
||||||
|
std::lock_guard<std::mutex> lock(queueMutex);
|
||||||
|
jobQueue.push(std::move(function));
|
||||||
|
condition.notify_one();
|
||||||
|
}
|
||||||
|
|
||||||
|
// Wait until all work items have been finished
|
||||||
|
void wait()
|
||||||
|
{
|
||||||
|
std::unique_lock<std::mutex> lock(queueMutex);
|
||||||
|
condition.wait(lock, [this]() { return jobQueue.empty(); });
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
|
class ThreadPool
|
||||||
|
{
|
||||||
|
public:
|
||||||
|
std::vector<std::unique_ptr<Thread>> threads;
|
||||||
|
|
||||||
|
// Sets the number of threads to be allocted in this pool
|
||||||
|
void setThreadCount(uint32_t count)
|
||||||
|
{
|
||||||
|
threads.clear();
|
||||||
|
for (auto i = 0; i < count; i++)
|
||||||
|
{
|
||||||
|
threads.push_back(std::make_unique<Thread>());
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// Wait until all threads have finished their work items
|
||||||
|
void wait()
|
||||||
|
{
|
||||||
|
for (auto &thread : threads)
|
||||||
|
{
|
||||||
|
thread->wait();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
|
}
|
||||||
Loading…
Add table
Add a link
Reference in a new issue