VOX
A little voxel engine
Loading...
Searching...
No Matches
ThreadPool.hpp
Go to the documentation of this file.
1#pragma once
2
3#include "define.hpp"
4
5#include <thread>
6#include <queue>
7#include <functional>
8#include <mutex>
9#include <condition_variable>
10#include <future>
11#include "Tracy.hpp"
12#include "tracy_globals.hpp"
13#include "JoinThreads.hpp"
14
16{
17public:
19
21 {
22 static ThreadPool instance;
23 return instance;
24 }
25
26 std::future<void> submit(std::function<void()> f)
27 {
28 std::unique_lock<std::mutex> lock(m_queue_mutex);
29 std::packaged_task<void()> task(std::move(f));
30 std::future<void> res(task.get_future());
31 m_work_queue.push(std::move(task));
32 m_cond.notify_one();
33 return res;
34 }
35
36private:
37 ThreadPool();
38 std::atomic_bool m_done;
39 std::queue<std::packaged_task<void()> > m_work_queue;
40 std::mutex m_queue_mutex;
41 std::condition_variable m_cond;
42 std::vector<std::thread> m_threads;
43 JoinThreads m_joiner;
44
45 void worker_thread(const int & id);
46};
47
Definition: JoinThreads.hpp:8
Definition: ThreadPool.hpp:16
static ThreadPool & get_instance()
Definition: ThreadPool.hpp:20
~ThreadPool()
Definition: ThreadPool.cpp:23
std::future< void > submit(std::function< void()> f)
Definition: ThreadPool.hpp:26
Definition: Executor.cpp:5