always use packaged_task for thread_pool push
parent
e50a86f339
commit
c1c68fb339
|
@ -11,6 +11,7 @@
|
||||||
#include <utility>
|
#include <utility>
|
||||||
#include <vector>
|
#include <vector>
|
||||||
#include <queue>
|
#include <queue>
|
||||||
|
#include <memory>
|
||||||
#include <thread>
|
#include <thread>
|
||||||
#include <future>
|
#include <future>
|
||||||
#include <mutex>
|
#include <mutex>
|
||||||
|
@ -18,13 +19,6 @@
|
||||||
|
|
||||||
namespace ostd {
|
namespace ostd {
|
||||||
|
|
||||||
namespace detail {
|
|
||||||
template<typename T>
|
|
||||||
using task_result_of = std::conditional_t<
|
|
||||||
std::is_same_v<T, void>, void, std::future<T>
|
|
||||||
>;
|
|
||||||
}
|
|
||||||
|
|
||||||
struct thread_pool {
|
struct thread_pool {
|
||||||
void start(size_t size = std::thread::hardware_concurrency()) {
|
void start(size_t size = std::thread::hardware_concurrency()) {
|
||||||
p_running = true;
|
p_running = true;
|
||||||
|
@ -61,49 +55,28 @@ struct thread_pool {
|
||||||
|
|
||||||
template<typename F, typename ...A>
|
template<typename F, typename ...A>
|
||||||
auto push(F &&func, A &&...args) ->
|
auto push(F &&func, A &&...args) ->
|
||||||
detail::task_result_of<std::result_of_t<F(A...)>>
|
std::future<std::result_of_t<F(A...)>>
|
||||||
{
|
{
|
||||||
using R = std::result_of_t<F(A...)>;
|
using R = std::result_of_t<F(A...)>;
|
||||||
if constexpr(std::is_same_v<R, void>) {
|
/* TODO: we can ditch the shared_ptr by implementing our own
|
||||||
/* void-returning funcs return void */
|
* move-only backing representation to replace use of std::function
|
||||||
{
|
*/
|
||||||
std::lock_guard<std::mutex> l{p_lock};
|
auto t = std::make_shared<std::packaged_task<R()>>(
|
||||||
if (!p_running) {
|
|
||||||
throw std::runtime_error{"push on stopped thread_pool"};
|
|
||||||
}
|
|
||||||
if constexpr(sizeof...(A) == 0) {
|
|
||||||
p_tasks.push(std::forward<F>(func));
|
|
||||||
} else {
|
|
||||||
p_tasks.push(
|
|
||||||
std::bind(std::forward<F>(func), std::forward<A>(args)...)
|
std::bind(std::forward<F>(func), std::forward<A>(args)...)
|
||||||
);
|
);
|
||||||
}
|
auto ret = t->get_future();
|
||||||
}
|
|
||||||
p_cond.notify_one();
|
|
||||||
} else {
|
|
||||||
/* non-void-returning funcs return a future */
|
|
||||||
std::packaged_task<R> t;
|
|
||||||
if constexpr(sizeof...(A) == 0) {
|
|
||||||
t = std::packaged_task<R>{std::forward<F>(func)};
|
|
||||||
} else {
|
|
||||||
t = std::packaged_task<R>{
|
|
||||||
std::bind(std::forward<F>(func), std::forward<A>(args)...)
|
|
||||||
};
|
|
||||||
}
|
|
||||||
auto ret = t.get_future();
|
|
||||||
{
|
{
|
||||||
std::lock_guard<std::mutex> l{p_lock};
|
std::lock_guard<std::mutex> l{p_lock};
|
||||||
if (!p_running) {
|
if (!p_running) {
|
||||||
throw std::runtime_error{"push on stopped thread_pool"};
|
throw std::runtime_error{"push on stopped thread_pool"};
|
||||||
}
|
}
|
||||||
p_tasks.emplace([t = std::move(t)]() {
|
p_tasks.emplace([t = std::move(t)]() mutable {
|
||||||
t();
|
(*t)();
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
p_cond.notify_one();
|
p_cond.notify_one();
|
||||||
return ret;
|
return ret;
|
||||||
}
|
}
|
||||||
}
|
|
||||||
|
|
||||||
private:
|
private:
|
||||||
void thread_run() {
|
void thread_run() {
|
||||||
|
|
Loading…
Reference in New Issue