Monero
Loading...
Searching...
No Matches
threadpool.h
Go to the documentation of this file.
1// Copyright (c) 2017-2022, 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 <deque>
35#include <functional>
36#include <utility>
37#include <vector>
38#include <stdexcept>
39
40namespace tools
41{
44{
45public:
47 static threadpool instance;
48 return instance;
49 }
51 static threadpool instance(8);
52 return instance;
53 }
54 static threadpool *getNewForUnitTests(unsigned max_threads = 0) {
55 return new threadpool(max_threads);
56 }
57
58 // The waiter lets the caller know when all of its
59 // tasks are completed.
60 class waiter {
61 boost::mutex mt;
62 boost::condition_variable cv;
64 int num;
66 public:
67 void inc();
68 void dec();
69 bool wait();
70 void set_error() noexcept { error_flag = true; }
71 bool error() const noexcept { return error_flag; }
73 ~waiter();
74 };
75
76 // Submit a task to the pool. The waiter pointer may be
77 // NULL if the caller doesn't care to wait for the
78 // task to finish.
79 void submit(waiter *waiter, std::function<void()> f, bool leaf = false);
80
81 // destroy and recreate threads
82 void recycle();
83
84 unsigned int get_max_concurrency() const;
85
87
88 private:
89 threadpool(unsigned int max_threads = 0);
90 void destroy();
91 void create(unsigned int max_threads);
92 typedef struct entry {
94 std::function<void()> f;
95 bool leaf;
97 std::deque<entry> queue;
98 boost::condition_variable has_work;
99 boost::mutex mutex;
100 std::vector<boost::thread> threads;
101 unsigned int active;
102 unsigned int max;
104 void run(bool flush = false);
105};
106
107}
Definition threadpool.h:60
waiter(threadpool &pool)
Definition threadpool.h:72
void set_error() noexcept
Wait for a set of tasks to finish, returns false iff any error.
Definition threadpool.h:70
int num
Definition threadpool.h:64
void inc()
Definition threadpool.cpp:139
bool wait()
Definition threadpool.cpp:131
bool error() const noexcept
Definition threadpool.h:71
void dec()
Definition threadpool.cpp:144
boost::condition_variable cv
Definition threadpool.h:62
bool error_flag
Definition threadpool.h:65
threadpool & pool
Definition threadpool.h:63
~waiter()
Definition threadpool.cpp:112
boost::mutex mt
Definition threadpool.h:61
unsigned int active
Definition threadpool.h:101
void run(bool flush=false)
Definition threadpool.cpp:151
void recycle()
Definition threadpool.cpp:68
static threadpool & getInstanceForCompute()
Definition threadpool.h:46
struct tools::threadpool::entry entry
threadpool(unsigned int max_threads=0)
Definition threadpool.cpp:39
std::deque< entry > queue
Definition threadpool.h:97
bool running
Definition threadpool.h:103
~threadpool()
Definition threadpool.cpp:43
unsigned int max
Definition threadpool.h:102
boost::condition_variable has_work
Definition threadpool.h:98
boost::mutex mutex
Definition threadpool.h:99
static threadpool * getNewForUnitTests(unsigned max_threads=0)
Definition threadpool.h:54
void submit(waiter *waiter, std::function< void()> f, bool leaf=false)
Definition threadpool.cpp:85
std::vector< boost::thread > threads
Definition threadpool.h:100
void destroy()
Definition threadpool.cpp:47
static threadpool & getInstanceForIO()
Definition threadpool.h:50
unsigned int get_max_concurrency() const
Definition threadpool.cpp:108
void create(unsigned int max_threads)
Definition threadpool.cpp:73
#define false
#define const
Definition ipfrdr.c:80
Various Tools.
Definition apply_permutation.h:40
Definition threadpool.h:92
bool leaf
Definition threadpool.h:95
waiter * wo
Definition threadpool.h:93
std::function< void()> f
Definition threadpool.h:94