Electroneum
Toggle main menu visibility
Loading...
Searching...
No Matches
threadpool.h
Go to the documentation of this file.
1
// Copyright (c) 2017-2019, The Monero Project
2
//
3
// All rights reserved.
4
//
5
// Redistribution and use in source and binary forms, with or without modification, are
6
// permitted provided that the following conditions are met:
7
//
8
// 1. Redistributions of source code must retain the above copyright notice, this list of
9
// conditions and the following disclaimer.
10
//
11
// 2. Redistributions in binary form must reproduce the above copyright notice, this list
12
// of conditions and the following disclaimer in the documentation and/or other
13
// materials provided with the distribution.
14
//
15
// 3. Neither the name of the copyright holder nor the names of its contributors may be
16
// used to endorse or promote products derived from this software without specific
17
// prior written permission.
18
//
19
// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY
20
// EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
21
// MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL
22
// THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
23
// SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
24
// PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
25
// INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
26
// STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF
27
// THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
28
#pragma once
29
30
#include <boost/thread/condition_variable.hpp>
31
#include <boost/thread/mutex.hpp>
32
#include <boost/thread/thread.hpp>
33
#include <cstddef>
34
#include <functional>
35
#include <utility>
36
#include <vector>
37
#include <stdexcept>
38
#include <deque>
39
40
namespace
tools
41
{
43
class
threadpool
44
{
45
public
:
46
static
threadpool&
getInstance
() {
47
static
threadpool instance;
48
return
instance;
49
}
50
static
threadpool *
getNewForUnitTests
(
unsigned
max_threads = 0) {
51
return
new
threadpool(max_threads);
52
}
53
54
// The waiter lets the caller know when all of its
55
// tasks are completed.
56
class
waiter
{
57
boost::mutex mt;
58
boost::condition_variable cv;
59
int
num;
60
public
:
61
void
inc
();
62
void
dec
();
63
void
wait
(threadpool *tpool);
64
waiter
() : num(0){}
65
~waiter
();
66
};
67
68
// Submit a task to the pool. The waiter pointer may be
69
// NULL if the caller doesn't care to wait for the
70
// task to finish.
71
void
submit
(waiter *waiter, std::function<
void
()> f,
bool
leaf =
false
);
72
73
unsigned
int
get_max_concurrency
()
const
;
74
75
~threadpool
();
76
77
private
:
78
threadpool
(
unsigned
int
max_threads = 0);
79
typedef
struct
entry {
80
waiter *wo;
81
std::function<void()> f;
82
bool
leaf;
83
} entry;
84
std::deque<entry> queue;
85
boost::condition_variable has_work;
86
boost::mutex mutex;
87
std::vector<boost::thread> threads;
88
unsigned
int
active;
89
unsigned
int
max;
90
bool
running;
91
void
run(
bool
flush =
false
);
92
};
93
94
}
tools::threadpool::waiter::waiter
waiter()
Wait for a set of tasks to finish.
Definition
threadpool.h:64
tools::threadpool::waiter::inc
void inc()
Definition
threadpool.cpp:123
tools::threadpool::waiter::dec
void dec()
Definition
threadpool.cpp:128
tools::threadpool::waiter::wait
void wait(threadpool *tpool)
Definition
threadpool.cpp:115
tools::threadpool::waiter::~waiter
~waiter()
Definition
threadpool.cpp:96
tools::threadpool
A global thread pool.
Definition
threadpool.h:44
tools::threadpool::~threadpool
~threadpool()
Definition
threadpool.cpp:49
tools::threadpool::getNewForUnitTests
static threadpool * getNewForUnitTests(unsigned max_threads=0)
Definition
threadpool.h:50
tools::threadpool::submit
void submit(waiter *waiter, std::function< void()> f, bool leaf=false)
Definition
threadpool.cpp:69
tools::threadpool::get_max_concurrency
unsigned int get_max_concurrency() const
Definition
threadpool.cpp:92
tools::threadpool::getInstance
static threadpool & getInstance()
Definition
threadpool.h:46
tools
Various Tools.
Definition
tools.cpp:31
src
common
threadpool.h
Generated on
for Electroneum by
1.17.0