spdlog
Loading...
Searching...
No Matches
thread_pool-inl.h
Go to the documentation of this file.
1// Copyright(c) 2015-present, Gabi Melman & spdlog contributors.
2// Distributed under the MIT License (http://opensource.org/licenses/MIT)
3
4#pragma once
5
6#ifndef SPDLOG_HEADER_ONLY
8#endif
9
10#include <spdlog/common.h>
11#include <cassert>
12
13namespace spdlog {
14namespace details {
15
16SPDLOG_INLINE thread_pool::thread_pool(size_t q_max_items, size_t threads_n, std::function<void()> on_thread_start)
17 : q_(q_max_items)
18{
19 if (threads_n == 0 || threads_n > 1000)
20 {
21 throw_spdlog_ex("spdlog::thread_pool(): invalid threads_n param (valid "
22 "range is 1-1000)");
23 }
24 for (size_t i = 0; i < threads_n; i++)
25 {
26 threads_.emplace_back([this, on_thread_start] {
27 on_thread_start();
29 });
30 }
31}
32
33SPDLOG_INLINE thread_pool::thread_pool(size_t q_max_items, size_t threads_n)
34 : thread_pool(q_max_items, threads_n, [] {})
35{}
36
37// message all threads to terminate gracefully join them
39{
41 {
42 for (size_t i = 0; i < threads_.size(); i++)
43 {
45 }
46
47 for (auto &t : threads_)
48 {
49 t.join();
50 }
51 }
53}
54
56{
57 async_msg async_m(std::move(worker_ptr), async_msg_type::log, msg);
58 post_async_msg_(std::move(async_m), overflow_policy);
59}
60
62{
63 post_async_msg_(async_msg(std::move(worker_ptr), async_msg_type::flush), overflow_policy);
64}
65
70
72{
73 return q_.size();
74}
75
77{
78 if (overflow_policy == async_overflow_policy::block)
79 {
80 q_.enqueue(std::move(new_msg));
81 }
82 else
83 {
84 q_.enqueue_nowait(std::move(new_msg));
85 }
86}
87
92
93// process next message in the queue
94// return true if this thread should still be active (while no terminate msg
95// was received)
97{
98 async_msg incoming_async_msg;
99 bool dequeued = q_.dequeue_for(incoming_async_msg, std::chrono::seconds(10));
100 if (!dequeued)
101 {
102 return true;
103 }
104
105 switch (incoming_async_msg.msg_type)
106 {
107 case async_msg_type::log: {
108 incoming_async_msg.worker_ptr->backend_sink_it_(incoming_async_msg);
109 return true;
110 }
112 incoming_async_msg.worker_ptr->backend_flush_();
113 return true;
114 }
115
117 return false;
118 }
119
120 default: {
121 assert(false);
122 }
123 }
124
125 return true;
126}
127
128} // namespace details
129} // namespace spdlog
bool dequeue_for(T &popped_item, std::chrono::milliseconds wait_duration)
void post_log(async_logger_ptr &&worker_ptr, const details::log_msg &msg, async_overflow_policy overflow_policy)
thread_pool(size_t q_max_items, size_t threads_n, std::function< void()> on_thread_start)
void post_async_msg_(async_msg &&new_msg, async_overflow_policy overflow_policy)
void post_flush(async_logger_ptr &&worker_ptr, async_overflow_policy overflow_policy)
std::vector< std::thread > threads_
#define SPDLOG_CATCH_STD
Definition common.h:88
#define SPDLOG_TRY
Definition common.h:86
#define SPDLOG_INLINE
Definition common.h:33
T emplace_back(T... args)
Definition async.h:25
async_overflow_policy
SPDLOG_INLINE void throw_spdlog_ex(const std::string &msg, int last_errno)
Definition common-inl.h:68
T size(T... args)
async_logger_ptr worker_ptr
Definition thread_pool.h:35