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 131 of file BoundedThreadPool.hpp.

131  : limit(8)
132  {
133  init(n_threads, limit);
134  }
void init(int n_threads, int limit)

◆ BoundedThreadPool() [2/3]

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

Definition at line 136 of file BoundedThreadPool.hpp.

137  {
138  init(n_threads, limit);
139  }

◆ BoundedThreadPool() [3/3]

BoundedThreadPool::BoundedThreadPool ( )
inline

Definition at line 141 of file BoundedThreadPool.hpp.

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

◆ ~BoundedThreadPool()

BoundedThreadPool::~BoundedThreadPool ( )
inline

Definition at line 143 of file BoundedThreadPool.hpp.

144  {
146  b_running = false;
147  cnd_buffer_empty.notify_all();
148  join_threads();
149  }

Member Function Documentation

◆ init()

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

Definition at line 115 of file BoundedThreadPool.hpp.

116  {
117  int n_threads_max = std::thread::hardware_concurrency();
118  if (n_threads > n_threads_max || n_threads < 1)
119  {
120  this->n_threads = n_threads_max;
121  }
122  else
123  {
124  this->n_threads = n_threads;
125  }
126  this->limit = limit;
127  b_running = true;
128  create_threads();
129  }

◆ join_threads()

void BoundedThreadPool::join_threads ( )
inline

Definition at line 107 of file BoundedThreadPool.hpp.

108  {
109  for (int i = 0; i < n_threads; i++)
110  {
111  threads[i].join();
112  }
113  }

◆ push_task() [1/2]

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

Definition at line 84 of file BoundedThreadPool.hpp.

85  {
86  std::unique_lock<std::mutex> lock(mtx_queue_full);
87  cnd_buffer_full.wait(lock, [this]
88  { return ((int)tasks.size() < limit); });
89  tasks.push(std::function<void()>(task));
90  cnd_buffer_empty.notify_one();
91  }

◆ push_task() [2/2]

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

Definition at line 94 of file BoundedThreadPool.hpp.

95  {
96  push_task([task, args...]
97  { task(args...); });
98  }
void push_task(const T &task)

◆ wait_for_completion()

void BoundedThreadPool::wait_for_completion ( )
inline

Definition at line 100 of file BoundedThreadPool.hpp.

101  {
102  std::unique_lock<std::mutex> lock(mtx_queue_full);
103  cnd_buffer_full.wait(lock, [this]
104  { return tasks.empty(); });
105  }

Member Data Documentation

◆ limit

int BoundedThreadPool::limit

Definition at line 81 of file BoundedThreadPool.hpp.

◆ n_threads

int BoundedThreadPool::n_threads

Definition at line 80 of file BoundedThreadPool.hpp.


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