Core Library  1.7.0.0
Library containing core utilities and tools for threading, networking, logging, INI and CSV file management etc.
EventThread.h
Go to the documentation of this file.
1 // This file is part of CoreLibrary containing useful reusable utility
2 // classes.
3 //
4 // Copyright (C) 2014 to present, Duncan Crutchley
5 // Contact <dac1976github@outlook.com>
6 //
7 // This program is free software: you can redistribute it and/or modify
8 // it under the terms of the GNU Lesser General Public License as published
9 // by the Free Software Foundation, either version 3 of the License, or
10 // (at your option) any later version.
11 //
12 // This program is distributed in the hope that it will be useful,
13 // but WITHOUT ANY WARRANTY; without even the implied warranty of
14 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 // GNU General Public License and GNU Lesser General Public License
16 // for more details.
17 //
18 // You should have received a copy of the GNU General Public License
19 // and GNU Lesser General Public License along with this program. If
20 // not, see <http://www.gnu.org/licenses/>.
21 
26 #ifndef EVENTTHREAD_H
27 #define EVENTTHREAD_H
28 
29 #include <functional>
30 #include "Threads/ThreadBase.h"
31 #include "Threads/SyncEvent.h"
32 
34 namespace core_lib
35 {
37 namespace threads
38 {
39 
42 class CORE_LIBRARY_DLL_SHARED_API EventThread final : public core_lib::threads::ThreadBase
43 {
45  using event_callback_t = std::function<void()>;
46 
47 public:
56  EventThread(event_callback_t const& eventCallback, unsigned int eventPeriodMillisecs,
57  bool delayedStart = false);
58 
60  ~EventThread() override;
61 
63  EventThread(const EventThread&) = delete;
65  EventThread& operator=(const EventThread&) = delete;
67  EventThread(EventThread&&) = delete;
69  EventThread& operator=(EventThread&&) = delete;
70 
75  void EventPeriod(unsigned int eventPeriodMillisecs);
80  unsigned int EventPeriod() const;
81 
83  void ForceTick();
84 
85 private:
87  void ThreadIteration() NO_EXCEPT_ override;
89  void ProcessTerminationConditions() NO_EXCEPT_ override;
90 
91 private:
93  mutable std::mutex m_eventTickMutex;
95  core_lib::threads::SyncEvent m_updateEvent{};
97  event_callback_t m_eventCallback{};
99  unsigned int m_eventPeriodMillisecs{0};
100 };
101 
102 } // namespace threads
103 } // namespace core_lib
104 
105 #endif // EVENTTHREAD_H
Class defining an EventThread that ticks at a given rate and executes a registered callback...
Definition: EventThread.h:42
Thread base class.
Definition: ThreadBase.h:49
The std namespace.
Definition: DebugLog.h:81
std::function< void()> event_callback_t
Typedef defining message handler functor.
Definition: EventThread.h:45
File containing declaration of ThreadBase class.
Class defining a thread synchronisation event.
Definition: SyncEvent.h:70
The core_lib namespace.
Definition: AsioDefines.h:59
File containing declaration of SyncEvent class.
#define NO_EXCEPT_
NO_EXCEPT_ definition mapping to noexcept.
Definition: PlatformDefines.h:57