Barretenberg
The ZK-SNARK library at the core of Aztec
Loading...
Searching...
No Matches
thread_pool.cpp
Go to the documentation of this file.
1
2#ifndef NO_MULTITHREADING
3
4#include "thread_pool.hpp"
6namespace bb {
7
8ThreadPool::ThreadPool(size_t num_threads)
9{
10 workers.reserve(num_threads);
11 for (size_t i = 0; i < num_threads; ++i) {
12 workers.emplace_back(&ThreadPool::worker_loop, this, i);
13 }
14}
15
17{
18 {
20 stop = true;
21 }
22 condition.notify_all();
23 for (auto& worker : workers) {
24 worker.join();
25 }
26}
27
28void ThreadPool::enqueue(const std::function<void()>& task)
29{
30 {
32 tasks.push(task);
33 }
34 condition.notify_one();
35}
36
38{
40 finished_condition.wait(lock, [this] { return tasks.empty() && tasks_running == 0; });
41}
42
43void ThreadPool::worker_loop(size_t /*unused*/)
44{
45 // info("created worker ", worker_num);
46 while (true) {
47 std::function<void()> task;
48 {
50 condition.wait(lock, [this] { return !tasks.empty() || stop; });
51
52 if (tasks.empty() && stop) {
53 break;
54 }
55
56 task = tasks.front();
57 tasks.pop();
59 }
60 // info("worker ", worker_num, " processing a task!");
61 task();
62 // info("task done");
63 {
66 if (tasks.empty() && tasks_running == 0) {
67 // info("notifying main thread");
68 finished_condition.notify_all();
69 }
70 }
71 // info("worker ", worker_num, " done!");
72 }
73 // info("worker exit ", worker_num);
74}
75} // namespace bb
76
77#endif
std::mutex tasks_mutex
std::condition_variable finished_condition
std::vector< std::thread > workers
size_t num_threads()
std::atomic< size_t > tasks_running
std::condition_variable condition
void worker_loop(size_t thread_index)
void enqueue(const std::function< void()> &task)
std::queue< std::function< void()> > tasks
ThreadPool(size_t num_threads)
Entry point for Barretenberg command-line interface.
constexpr decltype(auto) get(::tuplet::tuple< T... > &&t) noexcept
Definition tuple.hpp:13