riCOM_cpp
This repository contains the C++ implementation of the riCOM (Real Time Centre Of Mass) algorithm for 4D Scanning electron microscopy.
Public Member Functions | Public Attributes | List of all members
BoundedThreadPool Class Reference

#include <BoundedThreadPool.hpp>

Public Member Functions

template<typename T >
void push_task (const T &task)
 
template<typename T , typename... A>
void push_task (const T &task, const A &...args)
 
void wait_for_completion ()
 
void join_threads ()
 
void init (int n_threads, int limit)
 
 BoundedThreadPool (int n_threads)
 
 BoundedThreadPool (int n_threads, int limit)
 
 BoundedThreadPool ()
 
 ~BoundedThreadPool ()
 

Public Attributes

int n_threads
 
int limit
 

Detailed Description

Definition at line 25 of file BoundedThreadPool.hpp.

Constructor & Destructor Documentation

◆ BoundedThreadPool() [1/3]

BoundedThreadPool::BoundedThreadPool ( int  n_threads)
inlineexplicit

Definition at line 124 of file BoundedThreadPool.hpp.

124  : limit(8)
125  {
126  init(n_threads, limit);
127  }
void init(int n_threads, int limit)

◆ BoundedThreadPool() [2/3]

BoundedThreadPool::BoundedThreadPool ( int  n_threads,
int  limit 
)
inlineexplicit

Definition at line 129 of file BoundedThreadPool.hpp.

130  {
131  init(n_threads, limit);
132  }

◆ BoundedThreadPool() [3/3]

BoundedThreadPool::BoundedThreadPool ( )
inline

Definition at line 134 of file BoundedThreadPool.hpp.

134 : b_running(false), n_threads(0), limit(0) {}

◆ ~BoundedThreadPool()

BoundedThreadPool::~BoundedThreadPool ( )
inline

Definition at line 136 of file BoundedThreadPool.hpp.

137  {
139  b_running = false;
140  cnd_buffer_empty.notify_all();
141  join_threads();
142  }

Member Function Documentation

◆ init()

void BoundedThreadPool::init ( int  n_threads,
int  limit 
)
inline

Definition at line 108 of file BoundedThreadPool.hpp.

109  {
110  int n_threads_max = std::thread::hardware_concurrency();
111  if (n_threads > n_threads_max || n_threads < 1)
112  {
113  this->n_threads = n_threads_max;
114  }
115  else
116  {
117  this->n_threads = n_threads;
118  }
119  this->limit = limit;
120  b_running = true;
121  create_threads();
122  }

◆ join_threads()

void BoundedThreadPool::join_threads ( )
inline

Definition at line 100 of file BoundedThreadPool.hpp.

101  {
102  for (int i = 0; i < n_threads; i++)
103  {
104  threads[i].join();
105  }
106  }

◆ push_task() [1/2]

template<typename T >
void BoundedThreadPool::push_task ( const T &  task)
inline

Definition at line 77 of file BoundedThreadPool.hpp.

78  {
79  std::unique_lock<std::mutex> lock(mtx_queue);
80  cnd_buffer_full.wait(lock, [this]
81  { return ((int)tasks.size() < limit); });
82  tasks.push(std::function<void()>(task));
83  cnd_buffer_empty.notify_one();
84  }

◆ push_task() [2/2]

template<typename T , typename... A>
void BoundedThreadPool::push_task ( const T &  task,
const A &...  args 
)
inline

Definition at line 87 of file BoundedThreadPool.hpp.

88  {
89  push_task([task, args...]
90  { task(args...); });
91  }
void push_task(const T &task)

◆ wait_for_completion()

void BoundedThreadPool::wait_for_completion ( )
inline

Definition at line 93 of file BoundedThreadPool.hpp.

94  {
95  std::unique_lock<std::mutex> lock(mtx_queue);
96  cnd_buffer_full.wait(lock, [this]
97  { return tasks.empty(); });
98  }

Member Data Documentation

◆ limit

int BoundedThreadPool::limit

Definition at line 74 of file BoundedThreadPool.hpp.

◆ n_threads

int BoundedThreadPool::n_threads

Definition at line 73 of file BoundedThreadPool.hpp.


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