X-Git-Url: https://git.stg.codes/stg.git/blobdiff_plain/1c529746ff07312e30e76fd933c628c658e3c77d..646c8fd6c0112573ba2aae7f165f5d48e849831e:/projects/stargazer/async_pool.cpp diff --git a/projects/stargazer/async_pool.cpp b/projects/stargazer/async_pool.cpp new file mode 100644 index 00000000..1d8fec0c --- /dev/null +++ b/projects/stargazer/async_pool.cpp @@ -0,0 +1,42 @@ +#include "async_pool.h" + +using STG::AsyncPool; + +AsyncPool& STG::AsyncPoolST::instance() +{ + static AsyncPool pool; + return pool; +} + +void AsyncPool::start() +{ + if (m_thread.joinable()) + return; + m_thread = std::jthread([this](auto token){ run(std::move(token)); }); +} + +void AsyncPool::stop() +{ + if (!m_thread.joinable()) + return; + m_thread.request_stop(); + m_cond.notify_all(); +} + +void AsyncPool::run(std::stop_token token) noexcept +{ + while (true) + { + Queue tasks; + { + std::unique_lock lock(m_mutex); + m_cond.wait(lock, [this, &token](){ return !m_tasks.empty() || token.stop_requested(); }); + if (token.stop_requested()) + return; + if (!m_tasks.empty()) + tasks.swap(m_tasks); + } + for (const auto& t : tasks) + t(); + } +}