cmake-template
Loading...
Searching...
No Matches
threadpool.hpp
Go to the documentation of this file.
1#pragma once
2
3#include <atomic>
4#include <condition_variable>
5#include <functional>
6#include <mutex>
7#include <queue>
8#include <thread>
9#include <vector>
10
11#include "exkit/scheduler.hpp"
12
13class ThreadPool : public IScheduler {
14public:
26 ThreadPool(size_t thread_count = std::thread::hardware_concurrency());
27
36
37 ThreadPool(const ThreadPool&) = delete;
38
40
41 ThreadPool& operator=(const ThreadPool&) = delete;
42
44
52 void shutdown() override;
53
64 void schedule(std::function<void()> task) override;
65
74 void wait() override;
75
76private:
83 void _worker_loop();
84
85private:
86 std::queue<std::function<void()>> _tasks;
87 std::condition_variable _condition;
88 std::vector<std::thread> _threads;
89 std::atomic<size_t> _active_tasks;
90 std::mutex _queue_mutex;
91 std::atomic<bool> _stop;
92};
Definition: scheduler.hpp:5
Definition: threadpool.hpp:13
ThreadPool(size_t thread_count=std::thread::hardware_concurrency())
Construct a new ThreadPool object.
~ThreadPool()
Destroy the ThreadPool object.
void schedule(std::function< void()> task) override
Enqueues a new task to be executed by the thread pool.
ThreadPool(const ThreadPool &)=delete
void shutdown() override
Shuts down the thread pool and stops all worker threads.
ThreadPool(ThreadPool &&)=delete
ThreadPool & operator=(ThreadPool &&)=delete
void wait() override
Waits for all currently enqueued tasks to be completed.
ThreadPool & operator=(const ThreadPool &)=delete