spdlog
Loading...
Searching...
No Matches
include
spdlog
details
periodic_worker-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
7
# include <
spdlog/details/periodic_worker.h
>
8
#endif
9
10
namespace
spdlog
{
11
namespace
details {
12
13
SPDLOG_INLINE
periodic_worker::periodic_worker
(
const
std::function
<
void
()> &callback_fun,
std::chrono::seconds
interval)
14
{
15
active_
= (interval >
std::chrono::seconds::zero
());
16
if
(!
active_
)
17
{
18
return
;
19
}
20
21
worker_thread_
=
std::thread
([
this
, callback_fun, interval]() {
22
for
(;;)
23
{
24
std::unique_lock<std::mutex>
lock(this->
mutex_
);
25
if
(this->
cv_
.
wait_for
(lock, interval, [
this
] { return !this->active_; }))
26
{
27
return
;
// active_ == false, so exit this thread
28
}
29
callback_fun();
30
}
31
});
32
}
33
34
// stop the worker thread and join it
35
SPDLOG_INLINE
periodic_worker::~periodic_worker
()
36
{
37
if
(
worker_thread_
.
joinable
())
38
{
39
{
40
std::lock_guard<std::mutex>
lock(
mutex_
);
41
active_
=
false
;
42
}
43
cv_
.
notify_one
();
44
worker_thread_
.
join
();
45
}
46
}
47
48
}
// namespace details
49
}
// namespace spdlog
spdlog::details::periodic_worker::worker_thread_
std::thread worker_thread_
Definition
periodic_worker.h:31
spdlog::details::periodic_worker::mutex_
std::mutex mutex_
Definition
periodic_worker.h:32
spdlog::details::periodic_worker::periodic_worker
periodic_worker(const std::function< void()> &callback_fun, std::chrono::seconds interval)
Definition
periodic_worker-inl.h:13
spdlog::details::periodic_worker::~periodic_worker
~periodic_worker()
Definition
periodic_worker-inl.h:35
spdlog::details::periodic_worker::active_
bool active_
Definition
periodic_worker.h:30
spdlog::details::periodic_worker::cv_
std::condition_variable cv_
Definition
periodic_worker.h:33
SPDLOG_INLINE
#define SPDLOG_INLINE
Definition
common.h:33
std::chrono::seconds
std::function
std::thread::join
T join(T... args)
std::thread::joinable
T joinable(T... args)
std::lock_guard
spdlog
Definition
async.h:25
std::condition_variable::notify_one
T notify_one(T... args)
periodic_worker.h
std::thread
std::unique_lock
std::condition_variable::wait_for
T wait_for(T... args)
std::chrono::seconds::zero
T zero(T... args)
Generated by
1.9.8