Skip to content

Instantly share code, notes, and snippets.

@ktnyt
Created July 9, 2019 08:22
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save ktnyt/3b4edd0df59118b5e8c70ae1c8616015 to your computer and use it in GitHub Desktop.
Save ktnyt/3b4edd0df59118b5e8c70ae1c8616015 to your computer and use it in GitHub Desktop.
C++14 Minimal Thread Pool
#ifndef __KTNYT_THREAD_POOL_HPP__
#define __KTNYT_THREAD_POOL_HPP__
#include <thread>
#include <functional>
#include <mutex>
#include <condition_variable>
#include <queue>
#include <vector>
namespace ktnyt {
class thread_pool {
public:
thread_pool(std::size_t size) : stop(false) {
for (std::size_t i = 0; i < size; ++i) {
workers.emplace_back([this] { spawn(); });
}
}
virtual ~thread_pool() {
if (!stop) join();
}
void post(std::function<void()> f) {
{
std::unique_lock<std::mutex> lock(mutex);
tasks.push(f);
}
condition.notify_one();
}
void join() {
{
std::unique_lock<std::mutex> lock(mutex);
stop = true;
}
condition.notify_all();
for (std::size_t i = 0; i < workers.size(); ++i) {
workers[i].join();
}
}
private:
void spawn() {
std::function<void()> task;
while (!stop) {
std::unique_lock<std::mutex> lock(mutex);
condition.wait(lock, [this]() {
return (!tasks.empty()) || (tasks.empty() && stop);
});
if (!tasks.empty()) {
task = std::move(tasks.front());
tasks.pop();
task();
}
}
}
std::vector<std::thread> workers;
std::queue<std::function<void()>> tasks;
std::mutex mutex;
std::condition_variable condition;
bool stop;
};
inline void dispatch(thread_pool& pool, std::function<void()> f) {
pool.post(f);
}
} // namespace ktnyt
#endif // __KTNYT_THREAD_POOL_HPP__
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment