MCPcopy Create free account
hub / github.com/Tom94/tev / ThreadPool

Class ThreadPool

include/tev/ThreadPool.h:37–196  ·  view source on GitHub ↗

Source from the content-addressed store, hash-verified

35namespace tev {
36
37class ThreadPool {
38public:
39 ThreadPool();
40 ThreadPool(size_t maxNumThreads, bool force = false);
41 virtual ~ThreadPool();
42
43 static ThreadPool& global() {
44 static ThreadPool pool;
45 return pool;
46 }
47
48 template <std::invocable F> auto enqueueTask(F&& f, int priority, bool stopToken = false) {
49 using return_type = std::invoke_result_t<F>;
50
51 const auto task = std::make_shared<std::packaged_task<return_type()>>(std::forward<F>(f));
52 auto res = task->get_future();
53
54 if (mShuttingDown) {
55 return res;
56 }
57
58 {
59 const std::scoped_lock lock{mTaskQueueMutex};
60 mTaskQueue.push({
61 .fun = [task]() { (*task)(); },
62 .priority = priority,
63 .stopToken = stopToken,
64 });
65 }
66
67 ++mNumTasksInSystem;
68 mTaskQueueSemaphore.signal();
69
70 return res;
71 }
72
73 auto enqueueStopToken() {
74 return enqueueTask([] {}, std::numeric_limits<int>::max(), true);
75 }
76
77 inline auto enqueueCoroutine(int priority) noexcept {
78 class Awaiter {
79 public:
80 Awaiter(ThreadPool* pool, int priority) : mPool{pool}, mPriority{priority} {}
81
82 bool await_ready() const noexcept { return false; }
83
84 // Suspend and enqueue coroutine continuation onto the threadpool
85 void await_suspend(std::coroutine_handle<> coroutine) noexcept { mPool->enqueueTask(coroutine, mPriority); }
86
87 void await_resume() const noexcept {}
88
89 private:
90 ThreadPool* mPool;
91 int mPriority;
92 };
93
94 return Awaiter{this, priority};

Callers 1

loadMethod · 0.85

Calls

no outgoing calls

Tested by

no test coverage detected