spdlog
Loading...
Searching...
No Matches
thread_pool.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
8#include <spdlog/details/os.h>
9
10#include <chrono>
11#include <memory>
12#include <thread>
13#include <vector>
14#include <functional>
15
16namespace spdlog {
17class async_logger;
18
19namespace details {
20
22
24{
25 log,
26 flush,
28};
29
30// Async msg to move to/from the queue
31// Movable only. should never be copied
33{
36
37 async_msg() = default;
38 ~async_msg() = default;
39
40 // should only be moved in or out of the queue..
41 async_msg(const async_msg &) = delete;
42
43// support for vs2013 move
44#if defined(_MSC_VER) && _MSC_VER <= 1800
45 async_msg(async_msg &&other)
46 : log_msg_buffer(std::move(other))
47 , msg_type(other.msg_type)
48 , worker_ptr(std::move(other.worker_ptr))
49 {}
50
52 {
53 *static_cast<log_msg_buffer *>(this) = std::move(other);
54 msg_type = other.msg_type;
55 worker_ptr = std::move(other.worker_ptr);
56 return *this;
57 }
58#else // (_MSC_VER) && _MSC_VER <= 1800
59 async_msg(async_msg &&) = default;
61#endif
62
63 // construct from log_msg with given type
66 , msg_type{the_type}
67 , worker_ptr{std::move(worker)}
68 {}
69
72 , msg_type{the_type}
73 , worker_ptr{std::move(worker)}
74 {}
75
76 explicit async_msg(async_msg_type the_type)
77 : async_msg{nullptr, the_type}
78 {}
79};
80
82{
83public:
86
87 thread_pool(size_t q_max_items, size_t threads_n, std::function<void()> on_thread_start);
88 thread_pool(size_t q_max_items, size_t threads_n);
89
90 // message all threads to terminate gracefully join them
92
93 thread_pool(const thread_pool &) = delete;
95
96 void post_log(async_logger_ptr &&worker_ptr, const details::log_msg &msg, async_overflow_policy overflow_policy);
97 void post_flush(async_logger_ptr &&worker_ptr, async_overflow_policy overflow_policy);
98 size_t overrun_counter();
99 size_t queue_size();
100
101private:
103
105
106 void post_async_msg_(async_msg &&new_msg, async_overflow_policy overflow_policy);
107 void worker_loop_();
108
109 // process next message in the queue
110 // return true if this thread should still be active (while no terminate msg
111 // was received)
112 bool process_next_msg_();
113};
114
115} // namespace details
116} // namespace spdlog
117
118#ifdef SPDLOG_HEADER_ONLY
119# include "thread_pool-inl.h"
120#endif
thread_pool(const thread_pool &)=delete
thread_pool & operator=(thread_pool &&)=delete
std::vector< std::thread > threads_
#define SPDLOG_API
Definition common.h:31
Definition async.h:25
async_overflow_policy
async_logger_ptr worker_ptr
Definition thread_pool.h:35
async_msg(async_msg &&)=default
async_msg(async_msg_type the_type)
Definition thread_pool.h:76
async_msg(async_logger_ptr &&worker, async_msg_type the_type, const details::log_msg &m)
Definition thread_pool.h:64
async_msg(const async_msg &)=delete
async_msg(async_logger_ptr &&worker, async_msg_type the_type)
Definition thread_pool.h:70
async_msg & operator=(async_msg &&)=default