2#ifndef NO_MULTITHREADING
6#include <condition_variable>
19 ThreadPool(
size_t num_threads);
20 ThreadPool(
const ThreadPool& other) =
delete;
21 ThreadPool(ThreadPool&& other) =
delete;
24 ThreadPool& operator=(
const ThreadPool& other) =
delete;
25 ThreadPool& operator=(ThreadPool&& other) =
delete;
27 void start_tasks(
size_t num_iterations,
const std::function<
void(
size_t)>& func)
32 num_iterations_ = num_iterations;
36 condition.notify_all();
42 complete_condition_.wait(lock, [
this] {
return complete_ == num_iterations_; });
47 std::vector<std::thread> workers;
48 std::mutex tasks_mutex;
49 std::function<void(
size_t)> task_;
50 size_t num_iterations_ = 0;
51 size_t iteration_ = 0;
65 if (iteration_ == num_iterations_) {
68 iteration = iteration_++;
73 if (++complete_ == num_iterations_) {
74 complete_condition_.notify_one();
82ThreadPool::ThreadPool(
size_t num_threads)
84 workers.reserve(num_threads);
85 for (
size_t i = 0; i < num_threads; ++i) {
86 workers.emplace_back(&ThreadPool::worker_loop,
this, i);
90ThreadPool::~ThreadPool()
96 condition.notify_all();
97 for (
auto& worker : workers) {
102void ThreadPool::worker_loop(
size_t )
108 condition.wait(lock, [
this] {
return (iteration_ < num_iterations_) || stop; });
132 bool expected =
false;
133 if (!nested.compare_exchange_strong(expected,
true)) {
134 throw_or_abort(
"Error: Nested parallel_for_mutex_pool calls are not allowed.");
137 pool.start_tasks(num_iterations, func);
Entry point for Barretenberg command-line interface.
void parallel_for_mutex_pool(size_t num_iterations, const std::function< void(size_t)> &func)
constexpr decltype(auto) get(::tuplet::tuple< T... > &&t) noexcept
void throw_or_abort(std::string const &err)