From 70869c6967e04d345d9b443be5452f7965ec06ed Mon Sep 17 00:00:00 2001 From: TheExileFox Date: Thu, 21 Dec 2023 17:44:26 +0100 Subject: [PATCH 1/5] Update to C++17 --- TinyThreadPool.h | 82 ++++++++++++++++++++++-------------------------- 1 file changed, 38 insertions(+), 44 deletions(-) diff --git a/TinyThreadPool.h b/TinyThreadPool.h index 6cad804..fcd7656 100644 --- a/TinyThreadPool.h +++ b/TinyThreadPool.h @@ -1,20 +1,23 @@ #ifndef THREAD_POOL_H #define THREAD_POOL_H -#include -#include -#include -#include -#include -#include -class TinyThreadPool { +// originally from https://github.com/xiaoshengMr/TinyThreadPool but updated for C++17 +// Changed 'result_of' to 'invoke_result_t' as it is not a member of 'std' in C++17 + +#include +#include +#include +#include +#include +#include +class TinyThreadPool { public: TinyThreadPool(size_t); - template - auto enqueue(F&& f, Args&& ... args) - ->std::future::type>; - ~TinyThreadPool(); + template + auto enqueue(F&& f, Args&&... args) -> std::future>; + ~TinyThreadPool(); + private: std::vector workers; std::queue> tasks; @@ -24,61 +27,52 @@ class TinyThreadPool { }; inline TinyThreadPool::TinyThreadPool(size_t threads) : stop(false) { - - for(size_t i = 0; i < threads; i++) { - - workers.emplace_back( - [this] - { - for(;;) { - std::function tasks; - { - std::unique_lock lock(this->queue_mutex); - this->condition.wait(lock, - [this]{ return this->stop || !this->tasks.empty();}); - if(this->stop && this->tasks.empty()) { - return; - } - tasks = std::move(this->tasks.front()); - this->tasks.pop(); + for (size_t i = 0; i < threads; i++) { + workers.emplace_back([this] { + for (;;) { + std::function task; + { + std::unique_lock lock(this->queue_mutex); + this->condition.wait(lock, [this] { return this->stop || !this->tasks.empty(); }); + if (this->stop && this->tasks.empty()) { + return; } - tasks(); + task = std::move(this->tasks.front()); + this->tasks.pop(); } + task(); } - ); - } + }); + } } -template -auto TinyThreadPool::enqueue(F&& f, Args&&... args) - -> std::future::type> -{ - using return_type = typename std::result_of::type; - auto task = std::make_shared >( - std::bind(std::forward(f), std::forward(args) ...) - ); +template +auto TinyThreadPool::enqueue(F&& f, Args&&... args) -> std::future> { + using return_type = std::invoke_result_t; + + auto task = std::make_shared>(std::bind(std::forward(f), std::forward(args)...)); std::future res = task->get_future(); { std::unique_lock lock(queue_mutex); - if(stop) { - throw std::runtime_error("enqueue on stop ThreadPool"); + if (stop) { + throw std::runtime_error("enqueue on stopped ThreadPool"); } - tasks.emplace([task](){(*task)();}); + tasks.emplace([task] { (*task)(); }); } condition.notify_one(); return res; } - inline TinyThreadPool::~TinyThreadPool() { { std::unique_lock lock(queue_mutex); stop = true; } condition.notify_all(); - for(std::thread &worker : workers) { + for (std::thread& worker : workers) { worker.join(); } } + #endif \ No newline at end of file From a7ed297ed1f7de0b6bcd59faf7b20a1f21364aff Mon Sep 17 00:00:00 2001 From: TheExileFox Date: Thu, 21 Dec 2023 17:45:01 +0100 Subject: [PATCH 2/5] Update README.md --- README.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/README.md b/README.md index c2f143a..8bd79cc 100644 --- a/README.md +++ b/README.md @@ -1,2 +1,2 @@ # TinyThreadPool -C++11 tiny thread pool +C++17 tiny thread pool From 626ccfd055aee3e996d295d3c5a6c4f8be71eea9 Mon Sep 17 00:00:00 2001 From: TheExileFox Date: Thu, 21 Dec 2023 17:45:57 +0100 Subject: [PATCH 3/5] Update README.md --- README.md | 2 ++ 1 file changed, 2 insertions(+) diff --git a/README.md b/README.md index 8bd79cc..a94abf5 100644 --- a/README.md +++ b/README.md @@ -1,2 +1,4 @@ # TinyThreadPool C++17 tiny thread pool + +Derrived from https://github.com/xiaoshengMr/TinyThreadPool (which is c++11) From ba3efa01bc3449c1851477acfc7c8d154826ba30 Mon Sep 17 00:00:00 2001 From: TheExileFox Date: Wed, 24 Apr 2024 09:31:26 +0200 Subject: [PATCH 4/5] Update TinyThreadPool.h --- TinyThreadPool.h | 5 ++--- 1 file changed, 2 insertions(+), 3 deletions(-) diff --git a/TinyThreadPool.h b/TinyThreadPool.h index fcd7656..601db3a 100644 --- a/TinyThreadPool.h +++ b/TinyThreadPool.h @@ -1,8 +1,7 @@ #ifndef THREAD_POOL_H #define THREAD_POOL_H -// originally from https://github.com/xiaoshengMr/TinyThreadPool but updated for C++17 -// Changed 'result_of' to 'invoke_result_t' as it is not a member of 'std' in C++17 +// Changes in 2023: Changed 'result_of' to 'invoke_result_t' as it is not a member of 'std' in C++17 #include #include @@ -75,4 +74,4 @@ inline TinyThreadPool::~TinyThreadPool() { } } -#endif \ No newline at end of file +#endif From 1eb11ffddbdfbf236970eb4e4dd9c236210d1dd2 Mon Sep 17 00:00:00 2001 From: TheExileFox Date: Wed, 24 Apr 2024 09:34:16 +0200 Subject: [PATCH 5/5] Update README.md --- README.md | 2 -- 1 file changed, 2 deletions(-) diff --git a/README.md b/README.md index a94abf5..8bd79cc 100644 --- a/README.md +++ b/README.md @@ -1,4 +1,2 @@ # TinyThreadPool C++17 tiny thread pool - -Derrived from https://github.com/xiaoshengMr/TinyThreadPool (which is c++11)