Electroneum
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 
39 namespace tools
40 {
43 {
44 public:
45  static threadpool& getInstance() {
46  static threadpool instance;
47  return instance;
48  }
49  static threadpool *getNewForUnitTests(unsigned max_threads = 0) {
50  return new threadpool(max_threads);
51  }
52 
53  // The waiter lets the caller know when all of its
54  // tasks are completed.
55  class waiter {
56  boost::mutex mt;
57  boost::condition_variable cv;
58  int num;
59  public:
60  void inc();
61  void dec();
62  void wait(threadpool *tpool);
63  waiter() : num(0){}
64  ~waiter();
65  };
66 
67  // Submit a task to the pool. The waiter pointer may be
68  // NULL if the caller doesn't care to wait for the
69  // task to finish.
70  void submit(waiter *waiter, std::function<void()> f, bool leaf = false);
71 
72  unsigned int get_max_concurrency() const;
73 
74  ~threadpool();
75 
76  private:
77  threadpool(unsigned int max_threads = 0);
78  typedef struct entry {
80  std::function<void()> f;
81  bool leaf;
82  } entry;
83  std::deque<entry> queue;
84  boost::condition_variable has_work;
85  boost::mutex mutex;
86  std::vector<boost::thread> threads;
87  unsigned int active;
88  unsigned int max;
89  bool running;
90  void run(bool flush = false);
91 };
92 
93 }
Definition: threadpool.h:55
waiter()
Wait for a set of tasks to finish.
Definition: threadpool.h:63
int num
Definition: threadpool.h:58
void inc()
Definition: threadpool.cpp:123
void dec()
Definition: threadpool.cpp:128
boost::condition_variable cv
Definition: threadpool.h:57
void wait(threadpool *tpool)
Definition: threadpool.cpp:115
~waiter()
Definition: threadpool.cpp:96
boost::mutex mt
Definition: threadpool.h:56
A global thread pool.
Definition: threadpool.h:43
unsigned int active
Definition: threadpool.h:87
void run(bool flush=false)
Definition: threadpool.cpp:135
struct tools::threadpool::entry entry
threadpool(unsigned int max_threads=0)
Definition: threadpool.cpp:39
std::deque< entry > queue
Definition: threadpool.h:83
static threadpool * getNewForUnitTests(unsigned max_threads=0)
Definition: threadpool.h:49
bool running
Definition: threadpool.h:89
~threadpool()
Definition: threadpool.cpp:49
unsigned int max
Definition: threadpool.h:88
boost::condition_variable has_work
Definition: threadpool.h:84
boost::mutex mutex
Definition: threadpool.h:85
static threadpool & getInstance()
Definition: threadpool.h:45
void submit(waiter *waiter, std::function< void()> f, bool leaf=false)
Definition: threadpool.cpp:69
std::vector< boost::thread > threads
Definition: threadpool.h:86
unsigned int get_max_concurrency() const
Definition: threadpool.cpp:92
Various Tools.
Definition: apply_permutation.h:40
Definition: threadpool.h:78
bool leaf
Definition: threadpool.h:81
waiter * wo
Definition: threadpool.h:79
std::function< void()> f
Definition: threadpool.h:80