From 1e78b77a579558db9cf8069547f74b115da7b6d8 Mon Sep 17 00:00:00 2001 From: Jay Date: Tue, 20 Dec 2022 10:43:26 +0900 Subject: [PATCH] Fix: Compile warning(using C++17+) --- ThreadPool.h | 24 ++++++++++++++++++------ 1 file changed, 18 insertions(+), 6 deletions(-) diff --git a/ThreadPool.h b/ThreadPool.h index 4183203..a725291 100644 --- a/ThreadPool.h +++ b/ThreadPool.h @@ -15,21 +15,25 @@ class ThreadPool { public: ThreadPool(size_t); template - auto enqueue(F&& f, Args&&... args) + auto enqueue(F&& f, Args&&... args) +#if __cplusplus >= 201703L + -> std::future::type>; +#else -> std::future::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 > 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) @@ -60,15 +64,23 @@ inline ThreadPool::ThreadPool(size_t threads) // add new work item to the pool template -auto ThreadPool::enqueue(F&& f, Args&&... args) - -> std::future::type> +auto ThreadPool::enqueue(F&& f, Args&&... args) +#if __cplusplus >= 201703L + -> std::future::type> +#else + -> std::future::type> +#endif // __cplusplus >= 201703L { +#if __cplusplus >= 201703L + using return_type = typename std::invoke_result::type; +#else using return_type = typename std::result_of::type; +#endif // __cplusplus >= 201703L auto task = std::make_shared< std::packaged_task >( std::bind(std::forward(f), std::forward(args)...) ); - + std::future res = task->get_future(); { std::unique_lock lock(queue_mutex);