OpenCBDC Transaction Processor
Loading...
Searching...
No Matches
thread_pool.hpp
Go to the documentation of this file.
1// Copyright (c) 2022 MIT Digital Currency Initiative,
2// Federal Reserve Bank of Boston
3// Distributed under the MIT software license, see the accompanying
4// file COPYING or http://www.opensource.org/licenses/mit-license.php.
5
6#ifndef OPENCBDC_TX_SRC_COMMON_THREAD_POOL_H_
7#define OPENCBDC_TX_SRC_COMMON_THREAD_POOL_H_
8
9#include "blocking_queue.hpp"
10
11#include <atomic>
12#include <thread>
13#include <vector>
14
15namespace cbdc {
17 public:
18 thread_pool() = default;
20
21 thread_pool(const thread_pool&) = delete;
22 auto operator=(const thread_pool&) -> thread_pool& = delete;
24 auto operator=(thread_pool&&) -> thread_pool& = delete;
25
26 void push(const std::function<void()>& fn);
27
28 private:
29 struct thread_type {
30 std::thread m_thread;
31 std::atomic_bool m_running{false};
32 blocking_queue<std::function<void()>> m_queue;
33 };
34
35 std::mutex m_mut;
36 std::vector<std::shared_ptr<thread_type>> m_threads;
37
38 static void thread_loop(const std::shared_ptr<thread_type>& thr);
39 };
40}
41
42#endif
Thread-safe producer-consumer FIFO queue supporting multiple concurrent producers and consumers.
auto operator=(const thread_pool &) -> thread_pool &=delete
thread_pool(thread_pool &&)=delete
void push(const std::function< void()> &fn)
auto operator=(thread_pool &&) -> thread_pool &=delete
thread_pool()=default
thread_pool(const thread_pool &)=delete