OpenCBDC Transaction Processor
Loading...
Searching...
No Matches
thread_pool.cpp
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
#include "
thread_pool.hpp
"
7
8
namespace
cbdc
{
9
thread_pool::~thread_pool
() {
10
std::unique_lock l(m_mut);
11
for
(
auto
& t : m_threads) {
12
t->m_queue.clear();
13
if
(t->m_thread.joinable()) {
14
t->m_thread.join();
15
}
16
}
17
}
18
19
void
thread_pool::push
(
const
std::function<
void
()>& fn) {
20
std::unique_lock l(m_mut);
21
auto
launched =
false
;
22
for
(
auto
& thr : m_threads) {
23
if
(thr->m_running) {
24
continue
;
25
}
26
27
thr->m_running =
true
;
28
thr->m_queue.push(fn);
29
launched =
true
;
30
break
;
31
}
32
if
(launched) {
33
return
;
34
}
35
36
auto
thr = std::make_shared<thread_type>();
37
m_threads.emplace_back(thr);
38
thr->m_running =
true
;
39
thr->m_thread = std::thread([thr]() {
40
thread_loop(thr);
41
});
42
thr->m_queue.push(fn);
43
}
44
45
void
thread_pool::thread_loop(
const
std::shared_ptr<thread_type>& thr) {
46
auto
f = std::function<void()>();
47
while
(thr->m_queue.pop(f)) {
48
f();
49
thr->m_running =
false
;
50
}
51
}
52
}
cbdc::thread_pool::push
void push(const std::function< void()> &fn)
Definition
thread_pool.cpp:19
cbdc::thread_pool::~thread_pool
~thread_pool()
Definition
thread_pool.cpp:9
cbdc
Definition
parsec/agent/client.cpp:11
thread_pool.hpp
src
util
common
thread_pool.cpp
Generated by
1.12.0