Public Types | Public Member Functions | Private Member Functions | Private Attributes | List of all members
spdlog::details::thread_pool Class Reference

#include <thread_pool.h>

Public Types

using item_type = async_msg
 
using q_type = details::mpmc_blocking_queue< item_type >
 

Public Member Functions

 thread_pool (size_t q_max_items, size_t threads_n)
 
 ~thread_pool ()
 
 thread_pool (const thread_pool &)=delete
 
thread_pooloperator= (thread_pool &&)=delete
 
void post_log (async_logger_ptr &&worker_ptr, details::log_msg &msg, async_overflow_policy overflow_policy)
 
void post_flush (async_logger_ptr &&worker_ptr, async_overflow_policy overflow_policy)
 
size_t overrun_counter ()
 

Private Member Functions

void post_async_msg_ (async_msg &&new_msg, async_overflow_policy overflow_policy)
 
void worker_loop_ ()
 
bool process_next_msg_ ()
 

Private Attributes

q_type q_
 
std::vector< std::thread > threads_
 

Detailed Description

Definition at line 118 of file thread_pool.h.

Member Typedef Documentation

Definition at line 121 of file thread_pool.h.

Definition at line 122 of file thread_pool.h.

Constructor & Destructor Documentation

spdlog::details::thread_pool::thread_pool ( size_t  q_max_items,
size_t  threads_n 
)
inline

Definition at line 124 of file thread_pool.h.

125  : q_(q_max_items)
126  {
127  // std::cout << "thread_pool() q_size_bytes: " << q_size_bytes <<
128  // "\tthreads_n: " << threads_n << std::endl;
129  if (threads_n == 0 || threads_n > 1000)
130  {
131  throw spdlog_ex("spdlog::thread_pool(): invalid threads_n param (valid "
132  "range is 1-1000)");
133  }
134  for (size_t i = 0; i < threads_n; i++)
135  {
136  threads_.emplace_back(&thread_pool::worker_loop_, this);
137  }
138  }
std::vector< std::thread > threads_
Definition: thread_pool.h:182
spdlog::details::thread_pool::~thread_pool ( )
inline

Definition at line 141 of file thread_pool.h.

142  {
143  try
144  {
145  for (size_t i = 0; i < threads_.size(); i++)
146  {
148  }
149 
150  for (auto &t : threads_)
151  {
152  t.join();
153  }
154  }
155  catch (...)
156  {
157  }
158  }
void post_async_msg_(async_msg &&new_msg, async_overflow_policy overflow_policy)
Definition: thread_pool.h:184
std::vector< std::thread > threads_
Definition: thread_pool.h:182
spdlog::details::thread_pool::thread_pool ( const thread_pool )
delete

Member Function Documentation

thread_pool& spdlog::details::thread_pool::operator= ( thread_pool &&  )
delete
size_t spdlog::details::thread_pool::overrun_counter ( )
inline

Definition at line 174 of file thread_pool.h.

void spdlog::details::thread_pool::post_async_msg_ ( async_msg &&  new_msg,
async_overflow_policy  overflow_policy 
)
inlineprivate

Definition at line 184 of file thread_pool.h.

185  {
186  if (overflow_policy == async_overflow_policy::block)
187  {
188  q_.enqueue(std::move(new_msg));
189  }
190  else
191  {
192  q_.enqueue_nowait(std::move(new_msg));
193  }
194  }
def move(depos, offset)
Definition: depos.py:107
void spdlog::details::thread_pool::post_flush ( async_logger_ptr &&  worker_ptr,
async_overflow_policy  overflow_policy 
)
inline

Definition at line 169 of file thread_pool.h.

170  {
171  post_async_msg_(async_msg(std::move(worker_ptr), async_msg_type::flush), overflow_policy);
172  }
void post_async_msg_(async_msg &&new_msg, async_overflow_policy overflow_policy)
Definition: thread_pool.h:184
def move(depos, offset)
Definition: depos.py:107
void spdlog::details::thread_pool::post_log ( async_logger_ptr &&  worker_ptr,
details::log_msg msg,
async_overflow_policy  overflow_policy 
)
inline

Definition at line 163 of file thread_pool.h.

164  {
165  async_msg async_m(std::move(worker_ptr), async_msg_type::log, msg);
166  post_async_msg_(std::move(async_m), overflow_policy);
167  }
void msg(const char *fmt,...)
Definition: message.cpp:107
void post_async_msg_(async_msg &&new_msg, async_overflow_policy overflow_policy)
Definition: thread_pool.h:184
def move(depos, offset)
Definition: depos.py:107
bool spdlog::details::thread_pool::process_next_msg_ ( )
inlineprivate

Definition at line 204 of file thread_pool.h.

205  {
206  async_msg incoming_async_msg;
207  bool dequeued = q_.dequeue_for(incoming_async_msg, std::chrono::seconds(10));
208  if (!dequeued)
209  {
210  return true;
211  }
212 
213  switch (incoming_async_msg.msg_type)
214  {
215  case async_msg_type::log:
216  {
217  auto msg = incoming_async_msg.to_log_msg();
218  incoming_async_msg.worker_ptr->backend_log_(msg);
219  return true;
220  }
222  {
223  incoming_async_msg.worker_ptr->backend_flush_();
224  return true;
225  }
226 
228  {
229  return false;
230  }
231  }
232  assert(false && "Unexpected async_msg_type");
233  return true;
234  }
void msg(const char *fmt,...)
Definition: message.cpp:107
bool dequeue_for(T &popped_item, std::chrono::milliseconds wait_duration)
second seconds
Alias for common language habits.
Definition: spacetime.h:83
void spdlog::details::thread_pool::worker_loop_ ( )
inlineprivate

Definition at line 196 of file thread_pool.h.

197  {
198  while (process_next_msg_()) {};
199  }

Member Data Documentation

q_type spdlog::details::thread_pool::q_
private

Definition at line 180 of file thread_pool.h.

std::vector<std::thread> spdlog::details::thread_pool::threads_
private

Definition at line 182 of file thread_pool.h.


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