28 #include <type_traits>
30 #ifndef SRC_XRDEC_XRDECTHREADPOOL_HH_
31 #define SRC_XRDEC_XRDECTHREADPOOL_HH_
43 template<
int ... Is>
struct sequence {};
46 template <
int ... Ns>
struct seq_gen;
49 template <
int I,
int ... Ns>
50 struct seq_gen<
I, Ns...>
52 using type =
typename seq_gen<
I - 1,
I - 1, Ns...>::type;
57 struct seq_gen<0, Ns...>
59 using type = sequence<Ns...>;
63 template <
typename FUNC,
typename TUPL,
int ... INDICES>
64 inline static auto tuple_call_impl( FUNC &func, TUPL &args, sequence<INDICES...> ) -> decltype( func( std::move( std::get<INDICES>( args ) )... ) )
66 return func( std::move( std::get<INDICES>( args ) )... );
70 template <
typename FUNC,
typename ... ARGs>
71 inline static auto tuple_call( FUNC &func, std::tuple<ARGs...> &tup ) ->decltype( tuple_call_impl( func, tup,
typename seq_gen<
sizeof...(ARGs)>::type{} ) )
73 return tuple_call_impl( func, tup,
typename seq_gen<
sizeof...(ARGs)>::type{} );
80 template<
typename FUNC,
typename RET,
typename ... ARGs>
86 static inline void RunImpl( FUNC func, std::tuple<ARGs...> &args, std::promise<void> &prms )
88 tuple_call( func, args );
96 template<
typename RETURN>
97 static inline void RunImpl( FUNC func, std::tuple<ARGs...> &args, std::promise<RETURN> &prms )
99 prms.set_value( tuple_call( func, args ) );
109 AnyJob( FUNC func, ARGs... args ) : func( std::move( func ) ),
110 args( std::tuple<ARGs...>( std::move( args )... ) )
117 void Run(
void *arg )
119 RunImpl( this->func, this->args, this->prms );
126 std::future<RET> GetFuture()
128 return prms.get_future();
134 std::tuple<ARGs...> args;
135 std::promise<RET> prms;
161 template<
typename FUNC,
typename ... ARGs>
162 inline std::future<std::invoke_result_t<FUNC, ARGs...>>
165 using RET = std::invoke_result_t<FUNC, ARGs...>;
166 auto *job =
new AnyJob<FUNC, RET, ARGs...>( func, std::move( args )... );
167 std::future<RET> ftr = job->GetFuture();
168 threadpool.
QueueJob( job,
nullptr );
bool Finalize()
Finalize the job manager, clear the queues.
bool Start()
Start the workers.
bool Initialize()
Initialize the job manager.
bool Stop()
Stop the workers.
void QueueJob(Job *job, void *arg=0)
Add a job to be run.
Interface for a job to be run by the job manager.
static ThreadPool & Instance()
Singleton access.
std::future< std::invoke_result_t< FUNC, ARGs... > > Execute(FUNC func, ARGs... args)
Schedule a functional (together with its arguments) for execution.