Skip to content
Merged
Show file tree
Hide file tree
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
2 changes: 0 additions & 2 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,7 @@ set(CMAKE_CXX_STANDARD 23)
add_executable(ThreadPool demo/main.cpp
src/threadpool.h
src/threadpool.cpp
tests/return_value_tests.h
tests/all_tests.h
tests/task_tests.h
tests/threadpool_tests.h
demo/dependency_demo.h
demo/demo.h
Expand Down
15 changes: 7 additions & 8 deletions demo/demo.h
Original file line number Diff line number Diff line change
Expand Up @@ -17,17 +17,16 @@ inline void fibonacci_example() {

threadpool tp(3);

std::vector<return_value_handle<int>> futures = {
tp.submit<int>( []() -> int { return recursive_fibonacci(20);} ),
tp.submit<int>( []() -> int { return recursive_fibonacci(30);} ),
tp.submit<int>( []() -> int { return recursive_fibonacci(40);} ),
};

std::vector<std::future<int>> futures;
futures.reserve(3);
futures.emplace_back( tp.submit( []() -> int { return recursive_fibonacci(10);}));
futures.emplace_back( tp.submit( []() -> int { return recursive_fibonacci(20);}));
futures.emplace_back( tp.submit( []() -> int { return recursive_fibonacci(30);}));
Comment on lines +22 to +24
Copy link

Copilot AI Jan 29, 2026

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The Fibonacci numbers were changed from computing fib(20), fib(30), fib(40) to computing fib(10), fib(20), fib(30). While this change makes the demo run faster, it significantly reduces the computational workload and may not effectively demonstrate the threadpool's ability to handle parallel CPU-intensive tasks. The original values provided a better stress test for the threadpool.

Copilot uses AI. Check for mistakes.
tp.shutdown();

for (int i=0; i<futures.size(); i++) {
const auto& f = futures[i];
if (f.is_valid()) {
auto& f = futures[i];
if (f.valid()) {
std::cout << "Result " << i << " " << f.get() << std::endl << std::flush;
} else {
std::cout << "Result " << i << " not available" << std::endl << std::flush;
Expand Down
2 changes: 1 addition & 1 deletion demo/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@

int main() {
all_tests();
fibonacci_example();
//fibonacci_example();
// dependency_dag_example();
// multiple_threadpool_example();

Expand Down
24 changes: 8 additions & 16 deletions src/threadpool.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -18,9 +18,10 @@ threadpool::threadpool(const int& n) {
lock.unlock();
break;
}
std::optional<task> opt_task = this->poll_task();
if (opt_task.has_value()) {
auto& task = opt_task.value();

if (!tasks.empty()) {
auto task = std::move(tasks.front());
tasks.pop();
Comment on lines +22 to +24
Copy link

Copilot AI Jan 29, 2026

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Potential use-after-move issue. After std::move(tasks.front()), the task is moved from the queue, but tasks.pop() is then called. While this pattern is safe for std::queue, there's a brief window where the task object has been moved but not yet popped. This is not a bug per se, but the more conventional pattern is to call pop() before or ensure the moved-from object isn't accessed. The current implementation is correct but could be clearer.

Copilot uses AI. Check for mistakes.
lock.unlock();
task();
}
Expand All @@ -30,6 +31,9 @@ threadpool::threadpool(const int& n) {
}
}




threadpool::~threadpool() {
std::unique_lock<std::mutex> lock(queue_stop_mutex);
if (!(m_Stop)) {
Expand Down Expand Up @@ -62,19 +66,7 @@ void threadpool::shutdown_now() {
}

[[nodiscard]]
int threadpool::queue_size() {
size_t threadpool::queue_size() {
std::lock_guard<std::mutex> lock(queue_stop_mutex);
return tasks.size();
}

// ============ THREADPOOL PRIVATE ============

std::optional<task> threadpool::poll_task() {
// No lock guard as the thread would already have the guard
if (tasks.empty()) {
return std::nullopt;
}
task front = tasks.front();
tasks.pop();
return front;
}
Loading