Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Replace deprecated functions on -std=c++17 #102

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
24 changes: 18 additions & 6 deletions ThreadPool.h
Original file line number Diff line number Diff line change
Expand Up @@ -15,21 +15,25 @@ class ThreadPool {
public:
ThreadPool(size_t);
template<class F, class... Args>
auto enqueue(F&& f, Args&&... args)
auto enqueue(F&& f, Args&&... args)
#if __cplusplus >= 201703L
-> std::future<typename std::invoke_result<F, Args...>::type>;
#else
-> std::future<typename std::result_of<F(Args...)>::type>;
#endif // __cplusplus >= 201703L
~ThreadPool();
private:
// need to keep track of threads so we can join them
std::vector< std::thread > workers;
// the task queue
std::queue< std::function<void()> > tasks;

// synchronization
std::mutex queue_mutex;
std::condition_variable condition;
bool stop;
};

// the constructor just launches some amount of workers
inline ThreadPool::ThreadPool(size_t threads)
: stop(false)
Expand Down Expand Up @@ -60,15 +64,23 @@ inline ThreadPool::ThreadPool(size_t threads)

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

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

std::future<return_type> res = task->get_future();
{
std::unique_lock<std::mutex> lock(queue_mutex);
Expand Down