Electroneum
tools::threadpool Class Reference

A global thread pool. More...

#include <threadpool.h>

Classes

class  waiter
 

Public Member Functions

void submit (waiter *waiter, std::function< void()> f, bool leaf=false)
 
unsigned int get_max_concurrency () const
 
 ~threadpool ()
 

Static Public Member Functions

static threadpoolgetInstance ()
 
static threadpoolgetNewForUnitTests (unsigned max_threads=0)
 

Detailed Description

A global thread pool.

Definition at line 43 of file threadpool.h.

Constructor & Destructor Documentation

◆ ~threadpool()

tools::threadpool::~threadpool ( )

Definition at line 49 of file threadpool.cpp.

49  {
50  try
51  {
52  const boost::unique_lock<boost::mutex> lock(mutex);
53  running = false;
54  has_work.notify_all();
55  }
56  catch (...)
57  {
58  // if the lock throws, we're just do it without a lock and hope,
59  // since the alternative is terminate
60  running = false;
61  has_work.notify_all();
62  }
63  for (size_t i = 0; i<threads.size(); i++) {
64  try { threads[i].join(); }
65  catch (...) { /* ignore */ }
66  }
67 }

Member Function Documentation

◆ get_max_concurrency()

unsigned int tools::threadpool::get_max_concurrency ( ) const

Definition at line 92 of file threadpool.cpp.

92  {
93  return max;
94 }
Here is the caller graph for this function:

◆ getInstance()

static threadpool& tools::threadpool::getInstance ( )
inlinestatic

Definition at line 46 of file threadpool.h.

46  {
47  static threadpool instance;
48  return instance;
49  }
Here is the caller graph for this function:

◆ getNewForUnitTests()

static threadpool* tools::threadpool::getNewForUnitTests ( unsigned  max_threads = 0)
inlinestatic

Definition at line 50 of file threadpool.h.

50  {
51  return new threadpool(max_threads);
52  }
Here is the caller graph for this function:

◆ submit()

void tools::threadpool::submit ( waiter waiter,
std::function< void()>  f,
bool  leaf = false 
)

Definition at line 69 of file threadpool.cpp.

69  {
70  CHECK_AND_ASSERT_THROW_MES(!is_leaf, "A leaf routine is using a thread pool");
71  boost::unique_lock<boost::mutex> lock(mutex);
72  if (!leaf && ((active == max && !queue.empty()) || depth > 0)) {
73  // if all available threads are already running
74  // and there's work waiting, just run in current thread
75  lock.unlock();
76  ++depth;
77  is_leaf = leaf;
78  f();
79  --depth;
80  is_leaf = false;
81  } else {
82  if (obj)
83  obj->inc();
84  if (leaf)
85  queue.push_front({obj, f, leaf});
86  else
87  queue.push_back({obj, f, leaf});
88  has_work.notify_one();
89  }
90 }
#define CHECK_AND_ASSERT_THROW_MES(expr, message)
Definition: misc_log_ex.h:173
Here is the caller graph for this function:

The documentation for this class was generated from the following files: