Add special task handler to improve single threaded case.

This commit is contained in:
Patrick Schneider 2025-09-26 14:33:16 +02:00
parent a4736467f2
commit c8c0f80286
9 changed files with 125 additions and 52 deletions

View File

@ -7,7 +7,10 @@ find_package(Threads REQUIRED)
add_executable(Sorter framework/runner.cpp add_executable(Sorter framework/runner.cpp
src/container.cpp src/container.hpp src/container.cpp src/container.hpp
src/sorter.cpp src/sorter.hpp src/sorter.cpp src/sorter.hpp
src/thread_pool.cpp src/thread_pool.hpp) src/thread_pool.cpp src/thread_pool.hpp
src/single_task_handler.cpp src/single_task_handler.hpp
src/task_handler.hpp
)
target_link_libraries(Sorter PUBLIC Threads::Threads) target_link_libraries(Sorter PUBLIC Threads::Threads)
target_compile_features(Sorter PRIVATE cxx_std_20) target_compile_features(Sorter PRIVATE cxx_std_20)
@ -53,6 +56,9 @@ add_custom_command(
${CMAKE_CURRENT_LIST_DIR}/src/sorter.hpp ${CMAKE_CURRENT_LIST_DIR}/src/sorter.hpp
${CMAKE_CURRENT_LIST_DIR}/src/thread_pool.cpp ${CMAKE_CURRENT_LIST_DIR}/src/thread_pool.cpp
${CMAKE_CURRENT_LIST_DIR}/src/thread_pool.hpp ${CMAKE_CURRENT_LIST_DIR}/src/thread_pool.hpp
${CMAKE_CURRENT_LIST_DIR}/src/src/single_task_handler.cpp
${CMAKE_CURRENT_LIST_DIR}/src/src/single_task_handler.hpp
${CMAKE_CURRENT_LIST_DIR}/src/src/task_handler.hpp
${plot_file} ${plot_file}
${CMAKE_CURRENT_LIST_DIR}/description.md ${CMAKE_CURRENT_LIST_DIR}/description.md
COMMENT "Creating submission" COMMENT "Creating submission"
@ -76,6 +82,9 @@ set(dist_file_list
src/sorter.hpp src/sorter.hpp
src/thread_pool.cpp src/thread_pool.cpp
src/thread_pool.hpp src/thread_pool.hpp
src/single_task_handler.cpp
src/single_task_handler.hpp
src/task_handler.hpp
) )
set(framework_dist_file "ae-sorting.zip") set(framework_dist_file "ae-sorting.zip")

View File

@ -1,30 +1,30 @@
RESULT name=sort n=100 t=1 iterations=18 durationNanoseconds=57856 totalDurationNanoseconds=1041410 constructorNanoseconds=513 totalConstructorNanoseconds=9250 RESULT name=sort n=100 t=1 iterations=95 durationNanoseconds=10561 totalDurationNanoseconds=1003361 constructorNanoseconds=460 totalConstructorNanoseconds=43740
RESULT name=sort n=1000 t=1 iterations=8 durationNanoseconds=126883 totalDurationNanoseconds=1015070 constructorNanoseconds=1475 totalConstructorNanoseconds=11800 RESULT name=sort n=1000 t=1 iterations=22 durationNanoseconds=46203 totalDurationNanoseconds=1016480 constructorNanoseconds=764 totalConstructorNanoseconds=16820
RESULT name=sort n=10001 t=1 iterations=2 durationNanoseconds=615565 totalDurationNanoseconds=1231131 constructorNanoseconds=30890 totalConstructorNanoseconds=61780 RESULT name=sort n=10001 t=1 iterations=3 durationNanoseconds=459633 totalDurationNanoseconds=1378901 constructorNanoseconds=18656 totalConstructorNanoseconds=55970
RESULT name=sort n=100000 t=1 iterations=1 durationNanoseconds=9488107 totalDurationNanoseconds=9488107 constructorNanoseconds=524961 totalConstructorNanoseconds=524961 RESULT name=sort n=100000 t=1 iterations=1 durationNanoseconds=8231415 totalDurationNanoseconds=8231415 constructorNanoseconds=514381 totalConstructorNanoseconds=514381
RESULT name=sort n=10000000 t=1 iterations=1 durationNanoseconds=1416091993 totalDurationNanoseconds=1416091993 constructorNanoseconds=50440746 totalConstructorNanoseconds=50440746 RESULT name=sort n=10000000 t=1 iterations=1 durationNanoseconds=744634713 totalDurationNanoseconds=744634713 constructorNanoseconds=51303216 totalConstructorNanoseconds=51303216
RESULT name=sort n=100 t=2 iterations=16 durationNanoseconds=63258 totalDurationNanoseconds=1012141 constructorNanoseconds=375 totalConstructorNanoseconds=6000 RESULT name=sort n=100 t=2 iterations=18 durationNanoseconds=58551 totalDurationNanoseconds=1053931 constructorNanoseconds=361 totalConstructorNanoseconds=6510
RESULT name=sort n=1000 t=2 iterations=7 durationNanoseconds=154110 totalDurationNanoseconds=1078770 constructorNanoseconds=2008 totalConstructorNanoseconds=14060 RESULT name=sort n=1000 t=2 iterations=9 durationNanoseconds=120015 totalDurationNanoseconds=1080141 constructorNanoseconds=1345 totalConstructorNanoseconds=12110
RESULT name=sort n=10001 t=2 iterations=3 durationNanoseconds=451387 totalDurationNanoseconds=1354161 constructorNanoseconds=19620 totalConstructorNanoseconds=58860 RESULT name=sort n=10001 t=2 iterations=3 durationNanoseconds=495777 totalDurationNanoseconds=1487331 constructorNanoseconds=19473 totalConstructorNanoseconds=58420
RESULT name=sort n=100000 t=2 iterations=1 durationNanoseconds=6236655 totalDurationNanoseconds=6236655 constructorNanoseconds=514650 totalConstructorNanoseconds=514650 RESULT name=sort n=100000 t=2 iterations=1 durationNanoseconds=7827986 totalDurationNanoseconds=7827986 constructorNanoseconds=508510 totalConstructorNanoseconds=508510
RESULT name=sort n=10000000 t=2 iterations=1 durationNanoseconds=1380325518 totalDurationNanoseconds=1380325518 constructorNanoseconds=50373886 totalConstructorNanoseconds=50373886 RESULT name=sort n=10000000 t=2 iterations=1 durationNanoseconds=1296871738 totalDurationNanoseconds=1296871738 constructorNanoseconds=49085315 totalConstructorNanoseconds=49085315
RESULT name=sort n=100 t=4 iterations=9 durationNanoseconds=118743 totalDurationNanoseconds=1068691 constructorNanoseconds=436 totalConstructorNanoseconds=3930 RESULT name=sort n=100 t=4 iterations=9 durationNanoseconds=118596 totalDurationNanoseconds=1067371 constructorNanoseconds=455 totalConstructorNanoseconds=4100
RESULT name=sort n=1000 t=4 iterations=4 durationNanoseconds=272115 totalDurationNanoseconds=1088461 constructorNanoseconds=2415 totalConstructorNanoseconds=9660 RESULT name=sort n=1000 t=4 iterations=5 durationNanoseconds=220166 totalDurationNanoseconds=1100831 constructorNanoseconds=2204 totalConstructorNanoseconds=11020
RESULT name=sort n=10001 t=4 iterations=2 durationNanoseconds=569255 totalDurationNanoseconds=1138510 constructorNanoseconds=29920 totalConstructorNanoseconds=59840 RESULT name=sort n=10001 t=4 iterations=2 durationNanoseconds=502925 totalDurationNanoseconds=1005851 constructorNanoseconds=29945 totalConstructorNanoseconds=59890
RESULT name=sort n=100000 t=4 iterations=1 durationNanoseconds=6598125 totalDurationNanoseconds=6598125 constructorNanoseconds=507180 totalConstructorNanoseconds=507180 RESULT name=sort n=100000 t=4 iterations=1 durationNanoseconds=5620814 totalDurationNanoseconds=5620814 constructorNanoseconds=528570 totalConstructorNanoseconds=528570
RESULT name=sort n=10000000 t=4 iterations=1 durationNanoseconds=1300242690 totalDurationNanoseconds=1300242690 constructorNanoseconds=50475097 totalConstructorNanoseconds=50475097 RESULT name=sort n=10000000 t=4 iterations=1 durationNanoseconds=1198704968 totalDurationNanoseconds=1198704968 constructorNanoseconds=50132516 totalConstructorNanoseconds=50132516
RESULT name=sort n=100 t=8 iterations=3 durationNanoseconds=347863 totalDurationNanoseconds=1043591 constructorNanoseconds=2706 totalConstructorNanoseconds=8120 RESULT name=sort n=100 t=8 iterations=5 durationNanoseconds=222358 totalDurationNanoseconds=1111791 constructorNanoseconds=1116 totalConstructorNanoseconds=5580
RESULT name=sort n=1000 t=8 iterations=2 durationNanoseconds=610620 totalDurationNanoseconds=1221241 constructorNanoseconds=10400 totalConstructorNanoseconds=20800 RESULT name=sort n=1000 t=8 iterations=3 durationNanoseconds=457883 totalDurationNanoseconds=1373650 constructorNanoseconds=3950 totalConstructorNanoseconds=11851
RESULT name=sort n=10001 t=8 iterations=2 durationNanoseconds=706495 totalDurationNanoseconds=1412991 constructorNanoseconds=29600 totalConstructorNanoseconds=59200 RESULT name=sort n=10001 t=8 iterations=2 durationNanoseconds=705680 totalDurationNanoseconds=1411361 constructorNanoseconds=30140 totalConstructorNanoseconds=60280
RESULT name=sort n=100000 t=8 iterations=1 durationNanoseconds=7387085 totalDurationNanoseconds=7387085 constructorNanoseconds=557391 totalConstructorNanoseconds=557391 RESULT name=sort n=100000 t=8 iterations=1 durationNanoseconds=6234564 totalDurationNanoseconds=6234564 constructorNanoseconds=530141 totalConstructorNanoseconds=530141
RESULT name=sort n=10000000 t=8 iterations=1 durationNanoseconds=1261560682 totalDurationNanoseconds=1261560682 constructorNanoseconds=49470756 totalConstructorNanoseconds=49470756 RESULT name=sort n=10000000 t=8 iterations=1 durationNanoseconds=1219668902 totalDurationNanoseconds=1219668902 constructorNanoseconds=50095186 totalConstructorNanoseconds=50095186
RESULT name=sort n=100 t=12 iterations=3 durationNanoseconds=432037 totalDurationNanoseconds=1296111 constructorNanoseconds=1170 totalConstructorNanoseconds=3510 RESULT name=sort n=100 t=12 iterations=3 durationNanoseconds=336683 totalDurationNanoseconds=1010050 constructorNanoseconds=1553 totalConstructorNanoseconds=4660
RESULT name=sort n=1000 t=12 iterations=1 durationNanoseconds=1092461 totalDurationNanoseconds=1092461 constructorNanoseconds=12880 totalConstructorNanoseconds=12880 RESULT name=sort n=1000 t=12 iterations=2 durationNanoseconds=709845 totalDurationNanoseconds=1419691 constructorNanoseconds=3685 totalConstructorNanoseconds=7370
RESULT name=sort n=10001 t=12 iterations=1 durationNanoseconds=1019941 totalDurationNanoseconds=1019941 constructorNanoseconds=54540 totalConstructorNanoseconds=54540 RESULT name=sort n=10001 t=12 iterations=1 durationNanoseconds=1008971 totalDurationNanoseconds=1008971 constructorNanoseconds=45810 totalConstructorNanoseconds=45810
RESULT name=sort n=100000 t=12 iterations=1 durationNanoseconds=7159465 totalDurationNanoseconds=7159465 constructorNanoseconds=536730 totalConstructorNanoseconds=536730 RESULT name=sort n=100000 t=12 iterations=1 durationNanoseconds=4991544 totalDurationNanoseconds=4991544 constructorNanoseconds=494530 totalConstructorNanoseconds=494530
RESULT name=sort n=10000000 t=12 iterations=1 durationNanoseconds=1503813105 totalDurationNanoseconds=1503813105 constructorNanoseconds=50150056 totalConstructorNanoseconds=50150056 RESULT name=sort n=10000000 t=12 iterations=1 durationNanoseconds=1295272477 totalDurationNanoseconds=1295272477 constructorNanoseconds=50175095 totalConstructorNanoseconds=50175095
RESULT name=sort n=100 t=16 iterations=3 durationNanoseconds=432706 totalDurationNanoseconds=1298120 constructorNanoseconds=3833 totalConstructorNanoseconds=11500 RESULT name=sort n=100 t=16 iterations=2 durationNanoseconds=668925 totalDurationNanoseconds=1337851 constructorNanoseconds=3290 totalConstructorNanoseconds=6580
RESULT name=sort n=1000 t=16 iterations=2 durationNanoseconds=784875 totalDurationNanoseconds=1569751 constructorNanoseconds=5285 totalConstructorNanoseconds=10570 RESULT name=sort n=1000 t=16 iterations=2 durationNanoseconds=828375 totalDurationNanoseconds=1656751 constructorNanoseconds=5660 totalConstructorNanoseconds=11320
RESULT name=sort n=10001 t=16 iterations=1 durationNanoseconds=1953311 totalDurationNanoseconds=1953311 constructorNanoseconds=59420 totalConstructorNanoseconds=59420 RESULT name=sort n=10001 t=16 iterations=1 durationNanoseconds=1319491 totalDurationNanoseconds=1319491 constructorNanoseconds=65160 totalConstructorNanoseconds=65160
RESULT name=sort n=100000 t=16 iterations=1 durationNanoseconds=6820104 totalDurationNanoseconds=6820104 constructorNanoseconds=524961 totalConstructorNanoseconds=524961 RESULT name=sort n=100000 t=16 iterations=1 durationNanoseconds=7145805 totalDurationNanoseconds=7145805 constructorNanoseconds=529550 totalConstructorNanoseconds=529550
RESULT name=sort n=10000000 t=16 iterations=1 durationNanoseconds=1352546567 totalDurationNanoseconds=1352546567 constructorNanoseconds=52707158 totalConstructorNanoseconds=52707158 RESULT name=sort n=10000000 t=16 iterations=1 durationNanoseconds=1483766932 totalDurationNanoseconds=1483766932 constructorNanoseconds=52444497 totalConstructorNanoseconds=52444497

View File

@ -0,0 +1,30 @@
RESULT name=sort n=100 t=1 iterations=18 durationNanoseconds=57856 totalDurationNanoseconds=1041410 constructorNanoseconds=513 totalConstructorNanoseconds=9250
RESULT name=sort n=1000 t=1 iterations=8 durationNanoseconds=126883 totalDurationNanoseconds=1015070 constructorNanoseconds=1475 totalConstructorNanoseconds=11800
RESULT name=sort n=10001 t=1 iterations=2 durationNanoseconds=615565 totalDurationNanoseconds=1231131 constructorNanoseconds=30890 totalConstructorNanoseconds=61780
RESULT name=sort n=100000 t=1 iterations=1 durationNanoseconds=9488107 totalDurationNanoseconds=9488107 constructorNanoseconds=524961 totalConstructorNanoseconds=524961
RESULT name=sort n=10000000 t=1 iterations=1 durationNanoseconds=1416091993 totalDurationNanoseconds=1416091993 constructorNanoseconds=50440746 totalConstructorNanoseconds=50440746
RESULT name=sort n=100 t=2 iterations=16 durationNanoseconds=63258 totalDurationNanoseconds=1012141 constructorNanoseconds=375 totalConstructorNanoseconds=6000
RESULT name=sort n=1000 t=2 iterations=7 durationNanoseconds=154110 totalDurationNanoseconds=1078770 constructorNanoseconds=2008 totalConstructorNanoseconds=14060
RESULT name=sort n=10001 t=2 iterations=3 durationNanoseconds=451387 totalDurationNanoseconds=1354161 constructorNanoseconds=19620 totalConstructorNanoseconds=58860
RESULT name=sort n=100000 t=2 iterations=1 durationNanoseconds=6236655 totalDurationNanoseconds=6236655 constructorNanoseconds=514650 totalConstructorNanoseconds=514650
RESULT name=sort n=10000000 t=2 iterations=1 durationNanoseconds=1380325518 totalDurationNanoseconds=1380325518 constructorNanoseconds=50373886 totalConstructorNanoseconds=50373886
RESULT name=sort n=100 t=4 iterations=9 durationNanoseconds=118743 totalDurationNanoseconds=1068691 constructorNanoseconds=436 totalConstructorNanoseconds=3930
RESULT name=sort n=1000 t=4 iterations=4 durationNanoseconds=272115 totalDurationNanoseconds=1088461 constructorNanoseconds=2415 totalConstructorNanoseconds=9660
RESULT name=sort n=10001 t=4 iterations=2 durationNanoseconds=569255 totalDurationNanoseconds=1138510 constructorNanoseconds=29920 totalConstructorNanoseconds=59840
RESULT name=sort n=100000 t=4 iterations=1 durationNanoseconds=6598125 totalDurationNanoseconds=6598125 constructorNanoseconds=507180 totalConstructorNanoseconds=507180
RESULT name=sort n=10000000 t=4 iterations=1 durationNanoseconds=1300242690 totalDurationNanoseconds=1300242690 constructorNanoseconds=50475097 totalConstructorNanoseconds=50475097
RESULT name=sort n=100 t=8 iterations=3 durationNanoseconds=347863 totalDurationNanoseconds=1043591 constructorNanoseconds=2706 totalConstructorNanoseconds=8120
RESULT name=sort n=1000 t=8 iterations=2 durationNanoseconds=610620 totalDurationNanoseconds=1221241 constructorNanoseconds=10400 totalConstructorNanoseconds=20800
RESULT name=sort n=10001 t=8 iterations=2 durationNanoseconds=706495 totalDurationNanoseconds=1412991 constructorNanoseconds=29600 totalConstructorNanoseconds=59200
RESULT name=sort n=100000 t=8 iterations=1 durationNanoseconds=7387085 totalDurationNanoseconds=7387085 constructorNanoseconds=557391 totalConstructorNanoseconds=557391
RESULT name=sort n=10000000 t=8 iterations=1 durationNanoseconds=1261560682 totalDurationNanoseconds=1261560682 constructorNanoseconds=49470756 totalConstructorNanoseconds=49470756
RESULT name=sort n=100 t=12 iterations=3 durationNanoseconds=432037 totalDurationNanoseconds=1296111 constructorNanoseconds=1170 totalConstructorNanoseconds=3510
RESULT name=sort n=1000 t=12 iterations=1 durationNanoseconds=1092461 totalDurationNanoseconds=1092461 constructorNanoseconds=12880 totalConstructorNanoseconds=12880
RESULT name=sort n=10001 t=12 iterations=1 durationNanoseconds=1019941 totalDurationNanoseconds=1019941 constructorNanoseconds=54540 totalConstructorNanoseconds=54540
RESULT name=sort n=100000 t=12 iterations=1 durationNanoseconds=7159465 totalDurationNanoseconds=7159465 constructorNanoseconds=536730 totalConstructorNanoseconds=536730
RESULT name=sort n=10000000 t=12 iterations=1 durationNanoseconds=1503813105 totalDurationNanoseconds=1503813105 constructorNanoseconds=50150056 totalConstructorNanoseconds=50150056
RESULT name=sort n=100 t=16 iterations=3 durationNanoseconds=432706 totalDurationNanoseconds=1298120 constructorNanoseconds=3833 totalConstructorNanoseconds=11500
RESULT name=sort n=1000 t=16 iterations=2 durationNanoseconds=784875 totalDurationNanoseconds=1569751 constructorNanoseconds=5285 totalConstructorNanoseconds=10570
RESULT name=sort n=10001 t=16 iterations=1 durationNanoseconds=1953311 totalDurationNanoseconds=1953311 constructorNanoseconds=59420 totalConstructorNanoseconds=59420
RESULT name=sort n=100000 t=16 iterations=1 durationNanoseconds=6820104 totalDurationNanoseconds=6820104 constructorNanoseconds=524961 totalConstructorNanoseconds=524961
RESULT name=sort n=10000000 t=16 iterations=1 durationNanoseconds=1352546567 totalDurationNanoseconds=1352546567 constructorNanoseconds=52707158 totalConstructorNanoseconds=52707158

View File

@ -0,0 +1,11 @@
#include "single_task_handler.hpp"
void SingleTaskHandler::add(std::function<void()> task) {
task();
}
uint32_t SingleTaskHandler::size() {
return 0;
}
bool SingleTaskHandler::isWorking() {
return false;
}

View File

@ -0,0 +1,8 @@
#pragma once
#include "task_handler.hpp"
class SingleTaskHandler: TaskHandler {
void add(std::function<void()> task);
uint32_t size();
bool isWorking();
};

View File

@ -1,5 +1,8 @@
#include "sorter.hpp" #include "sorter.hpp"
#include "single_task_handler.hpp"
#include "thread_pool.hpp"
#include <algorithm> #include <algorithm>
#include <iterator> #include <iterator>
#include <cassert> #include <cassert>
@ -13,7 +16,11 @@ namespace ae {
sorter::sorter(uint32_t num = 1) { sorter::sorter(uint32_t num = 1) {
sorter::num_threads = num; sorter::num_threads = num;
sorter::pool = new ThreadPool(num); if (num > 1) {
sorter::handler = (TaskHandler*) new ThreadPool(num);
} else {
sorter::handler = (TaskHandler*) new SingleTaskHandler();
}
} }
void sorter::sort(container& data) { void sorter::sort(container& data) {
@ -32,7 +39,7 @@ void sorter::sort(container& data) {
std::sort(copy.begin(), copy.end()); std::sort(copy.begin(), copy.end());
#endif #endif
sorter::msd_inplace_radix_sort(data.placeholder_[0], 0, [&](auto span) {sorter::robin_hood_sort(span);}); sorter::msd_inplace_radix_sort(data.placeholder_[0], 0, [&](auto span) {sorter::robin_hood_sort(span);});
while (sorter::pool->size() > 0 || sorter::pool->isWorking()) {}; while (sorter::handler->size() > 0 || sorter::handler->isWorking()) {};
#if DEBUG #if DEBUG
for (int i = 0; i < copy.size(); i++) { for (int i = 0; i < copy.size(); i++) {
if (copy[i] != data.placeholder_[0][i]) if (copy[i] != data.placeholder_[0][i])
@ -175,24 +182,20 @@ void sorter::msd_inplace_radix_sort(
// sort each bucket recursively // sort each bucket recursively
for (auto i = 0; i < sorter::RADIX_BUCKETS; i++) { for (auto i = 0; i < sorter::RADIX_BUCKETS; i++) {
if (sorter::pool != nullptr) { #if DEBUG
std::cerr << "Putting in task with depth " << passes << " of bucket " << i << std::endl;
#endif
auto start = buckets_start[i];
auto end = buckets_end[i];
sorter::handler->add([start, end, &bucket_sort, passes, this, i](){
#if DEBUG #if DEBUG
std::cerr << "Putting in task with depth " << passes << " of bucket " << i << std::endl; std::cerr << "Starting task with depth " << passes << " of bucket " << i << std::endl;
#endif #endif
auto start = buckets_start[i]; sorter::msd_inplace_radix_sort(std::span<container::element_type> (start, end), passes + 1, bucket_sort);
auto end = buckets_end[i]; #if DEBUG
sorter::pool->add([start, end, &bucket_sort, passes, this, i](){ std::cerr << "Finishing task with depth " << passes << " of bucket " << i << std::endl;
#if DEBUG #endif
std::cerr << "Starting task with depth " << passes << " of bucket " << i << std::endl; });
#endif
sorter::msd_inplace_radix_sort(std::span<container::element_type> (start, end), passes + 1, bucket_sort);
#if DEBUG
std::cerr << "Finishing task with depth " << passes << " of bucket " << i << std::endl;
#endif
});
} else {
sorter::msd_inplace_radix_sort(std::span<container::element_type> (buckets_start[i], buckets_end[i]), passes + 1, bucket_sort);
}
} }
} }

View File

@ -1,7 +1,7 @@
#pragma once #pragma once
#include "container.hpp" #include "container.hpp"
#include "thread_pool.hpp" #include "task_handler.hpp"
#include "functional" #include "functional"
#include "math.h" #include "math.h"
#include <mutex> #include <mutex>
@ -40,7 +40,7 @@ class sorter {
const uint32_t RADIX_BUCKETS = std::pow(2, 4); const uint32_t RADIX_BUCKETS = std::pow(2, 4);
const uint32_t RADIX_ITERATIONS = 8; const uint32_t RADIX_ITERATIONS = 8;
uint32_t num_threads; uint32_t num_threads;
ThreadPool* pool = nullptr; TaskHandler* handler = nullptr;
void robin_hood_sort(std::span<container::element_type> range); void robin_hood_sort(std::span<container::element_type> range);
}; };

11
src/task_handler.hpp Normal file
View File

@ -0,0 +1,11 @@
#pragma once
#include <functional>
#include <cstdint>
class TaskHandler {
public:
virtual void add(std::function<void()> task) = 0;
virtual uint32_t size() = 0;
virtual bool isWorking() = 0;
};

View File

@ -1,5 +1,6 @@
#pragma once #pragma once
#include "task_handler.hpp"
#include <algorithm> #include <algorithm>
#include <vector> #include <vector>
#include <mutex> #include <mutex>
@ -11,7 +12,7 @@
// This class was inspired by https://www.geeksforgeeks.org/cpp/thread-pool-in-cpp/ (access: 26/09/2025) // This class was inspired by https://www.geeksforgeeks.org/cpp/thread-pool-in-cpp/ (access: 26/09/2025)
// to more efficiently handle threads // to more efficiently handle threads
class ThreadPool { class ThreadPool: TaskHandler {
private: private:
std::vector<std::thread> threads; std::vector<std::thread> threads;
std::queue<std::function<void()>> tasks; std::queue<std::function<void()>> tasks;