lasp/cpp_src/dsp/lasp_thread.h

42 lines
1.1 KiB
C++

#pragma once
#include "BS_thread_pool.hpp"
/**
* @brief Simple wrapper around BS::thread_pool that makes a BS::thread_pool a
* singleton, such that a thread pool can be used around in the code, and
* safely spawn threads also from other threads. Only wraps a submit() and
* push_task for now.
*/
class GlobalThreadPool {
/**
* @brief Shared access to the thread pool.
*/
std::shared_ptr<BS::thread_pool> _pool;
public:
/**
* @brief Instantiate handle to the thread pool.
*/
GlobalThreadPool();
GlobalThreadPool(const GlobalThreadPool &) = default;
GlobalThreadPool &operator=(const GlobalThreadPool &) = default;
/**
* @brief Wrapper around BS::thread_pool::submit(...)
*/
template <
typename F, typename... A,
typename R = std::invoke_result_t<std::decay_t<F>, std::decay_t<A>...>>
[[nodiscard]] std::future<R> submit(F &&task, A &&...args) {
return _pool->submit(task, args...);
}
/**
* @brief Wrapper around BS::thread_pool::push_task(...)
*/
template <typename F, typename... A> void push_task(F &&task, A &&...args) {
_pool->push_task(task, args...);
}
};