Skip to content

Commit

Permalink
[upgrade] ThreadPool now supports C++20 (#195)
Browse files Browse the repository at this point in the history
  • Loading branch information
gconeice authored Oct 21, 2024
1 parent 62ee03a commit 88f722f
Showing 1 changed file with 13 additions and 0 deletions.
13 changes: 13 additions & 0 deletions emp-tool/utils/ThreadPool.h
Original file line number Diff line number Diff line change
Expand Up @@ -39,8 +39,13 @@ class ThreadPool {
public:
ThreadPool(size_t);
template<class F, class... Args>
#if __cplusplus >= 202002L
auto enqueue(F&& f, Args&&... args)
-> std::future<typename std::invoke_result<F,Args...>::type>;
#else
auto enqueue(F&& f, Args&&... args)
-> std::future<typename std::result_of<F(Args...)>::type>;
#endif
~ThreadPool();
int size() const;
private:
Expand Down Expand Up @@ -87,11 +92,19 @@ inline ThreadPool::ThreadPool(size_t threads)
}

// add new work item to the pool
#if __cplusplus >= 202002L
template<class F, class... Args>
auto ThreadPool::enqueue(F&& f, Args&&... args)
-> std::future<typename std::invoke_result<F,Args...>::type>
{
using return_type = typename std::invoke_result<F,Args...>::type;
#else
template<class F, class... Args>
auto ThreadPool::enqueue(F&& f, Args&&... args)
-> std::future<typename std::result_of<F(Args...)>::type>
{
using return_type = typename std::result_of<F(Args...)>::type;
#endif

auto task = std::make_shared< std::packaged_task<return_type()> >(
std::bind(std::forward<F>(f), std::forward<Args>(args)...)
Expand Down

0 comments on commit 88f722f

Please sign in to comment.