3 #ifndef ROBOWFLEX_POOL_
4 #define ROBOWFLEX_POOL_
24 struct function_traits :
public function_traits<decltype(&T::operator())>
29 template <
typename C,
typename RT,
typename... Args>
30 struct function_traits<RT (C::*)(Args...) const>
34 arity =
sizeof...(Args)
40 template <
typename C,
typename RT,
typename... Args>
41 struct function_traits<RT (C::*)(Args...)>
45 arity =
sizeof...(Args)
51 template <
typename RT,
typename... Args>
52 struct function_traits<RT (*)(Args...)>
56 arity =
sizeof...(Args)
62 static typename function_traits<L>::f_type make_function(L l)
64 return (
typename function_traits<L>::f_type)(l);
68 template <
typename RT,
typename... Args,
class T>
71 return {std::forward<T>(t)};
75 template <
typename RT,
typename... Args>
76 auto make_function(RT (*p)(Args...)) ->
std::function<RT(Args...)>
82 template <
typename RT,
typename... Args,
typename C>
83 auto make_function(RT (C::*p)(Args...)) ->
std::function<RT(Args...)>
122 template <
typename RT>
131 template <
typename... Args>
208 template <
typename RT,
typename... Args>
212 std::forward<Args>(args)...);
RT get()
Blocking call to retrieve the result of the function. Note that if the job was canceled it is not gua...
std::future< RT > future_
Future of function result.
std::function< RT()> function_
Bound function to execute.
bool isDone() const
Returns true if the task is done, false otherwise.
void wait() const
Waits until result of the job is available.
Job(const std::function< RT(Args...)> &&function, Args &&... args)
Constructor.
bool waitFor(double time) const
Waits for a number of seconds to see if the task completes.
std::packaged_task< RT()> task_
Task of function.
void execute() override
Executes the task and stores the result in future_.
Interface class for Pool::Job so template parameters are not needed for the queue.
virtual void execute()=0
Execute the underlying function.
bool isCancled() const
Checks if this job has been cancled.
bool canceled
Whether the job is cancled or not.
void cancel()
Cancels this job.
A thread pool that can execute arbitrary functions asynchronously. Functions with arguments to be exe...
std::mutex mutex_
Job queue mutex.
unsigned int getThreadCount() const
Get the number of threads.
bool active_
Is thread pool active?
~Pool()
Destructor. Cancels all threads and joins them.
Pool(unsigned int n=std::thread::hardware_concurrency())
Constructor.
std::condition_variable cv_
Job queue condition variable.
std::vector< std::thread > threads_
Threads.
void run()
Background thread process. Executes jobs submitted from submit().
std::queue< std::shared_ptr< Joblet > > jobs_
Jobs to execute.
std::shared_ptr< Job< RT > > submit(const std::function< RT(Args...)> &&function, Args &&... args) const
Submit a function with arguments to be processed by the thread pool. Submitted functions must be wrap...
T hardware_concurrency(T... args)
Main namespace. Contains all library classes and functions.