From 7745a6cc4208ef3ba4190922a6e82cb0131b7206 Mon Sep 17 00:00:00 2001 From: Paul T Date: Fri, 26 Apr 2024 09:52:07 -0400 Subject: [PATCH 1/4] feat: add initial version of thread init function Added initial version of thread initialization function. --- include/thread_pool/thread_pool.h | 10 ++++++++-- 1 file changed, 8 insertions(+), 2 deletions(-) diff --git a/include/thread_pool/thread_pool.h b/include/thread_pool/thread_pool.h index 8a8fe22..844b395 100644 --- a/include/thread_pool/thread_pool.h +++ b/include/thread_pool/thread_pool.h @@ -34,14 +34,20 @@ namespace dp { std::is_same_v> class thread_pool { public: + template > + requires std::invocable && + std::is_same_v> explicit thread_pool( - const unsigned int &number_of_threads = std::thread::hardware_concurrency()) + const unsigned int &number_of_threads = std::thread::hardware_concurrency(), + InitializationFunction init = [](std::size_t) {}) : tasks_(number_of_threads) { std::size_t current_id = 0; for (std::size_t i = 0; i < number_of_threads; ++i) { priority_queue_.push_back(size_t(current_id)); try { - threads_.emplace_back([&, id = current_id](const std::stop_token &stop_tok) { + threads_.emplace_back([&, id = current_id, + init](const std::stop_token &stop_tok) { + init(id); do { // wait until signaled tasks_[id].signal.acquire(); From 05d5ce3ec1413dcc8faa6e7f8eef023520d4e693 Mon Sep 17 00:00:00 2001 From: Paul T Date: Fri, 26 Apr 2024 09:52:35 -0400 Subject: [PATCH 2/4] test: add unit test for thread init function --- test/source/thread_pool.cpp | 12 ++++++++++++ 1 file changed, 12 insertions(+) diff --git a/test/source/thread_pool.cpp b/test/source/thread_pool.cpp index 9fe8f4f..5c563b8 100644 --- a/test/source/thread_pool.cpp +++ b/test/source/thread_pool.cpp @@ -6,6 +6,7 @@ #include #include #include +#include #include #include #include @@ -445,3 +446,14 @@ TEST_CASE("Ensure wait_for_tasks() properly blocks current execution.") { CHECK_EQ(counter.load(), total_tasks); } + +TEST_CASE("Initialization function is called") { + std::atomic_int counter = 0; + { + dp::thread_pool pool(4, [&counter](std::size_t id) { + std::print("Thread {} initialized\n", id); + counter.fetch_add(1); + }); + } + CHECK_EQ(counter.load(), 4); +} From 13b9c82c148d615b90293bc67d9645d0151004be Mon Sep 17 00:00:00 2001 From: Paul T Date: Fri, 26 Apr 2024 09:59:59 -0400 Subject: [PATCH 3/4] fix: isn't available everywhere --- test/source/thread_pool.cpp | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/test/source/thread_pool.cpp b/test/source/thread_pool.cpp index 5c563b8..275e1d4 100644 --- a/test/source/thread_pool.cpp +++ b/test/source/thread_pool.cpp @@ -4,9 +4,9 @@ #include #include +#include #include #include -#include #include #include #include @@ -451,7 +451,7 @@ TEST_CASE("Initialization function is called") { std::atomic_int counter = 0; { dp::thread_pool pool(4, [&counter](std::size_t id) { - std::print("Thread {} initialized\n", id); + std::cout << std::format("Thread {} initialized\n", id); counter.fetch_add(1); }); } From 99d2e15d4f400ae025d0cb0375b1574e610f93ac Mon Sep 17 00:00:00 2001 From: Paul T Date: Fri, 26 Apr 2024 11:06:38 -0400 Subject: [PATCH 4/4] fix: isn't available everywhere either... --- test/source/thread_pool.cpp | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/test/source/thread_pool.cpp b/test/source/thread_pool.cpp index 275e1d4..2c008a5 100644 --- a/test/source/thread_pool.cpp +++ b/test/source/thread_pool.cpp @@ -4,7 +4,6 @@ #include #include -#include #include #include #include @@ -451,7 +450,7 @@ TEST_CASE("Initialization function is called") { std::atomic_int counter = 0; { dp::thread_pool pool(4, [&counter](std::size_t id) { - std::cout << std::format("Thread {} initialized\n", id); + std::cout << "Thread " << id << " initialized\n"; counter.fetch_add(1); }); }