Core Library  1.7.0.0
Library containing core utilities and tools for threading, networking, logging, INI and CSV file management etc.
ThreadBase.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 
27 #ifndef THREADBASE
28 #define THREADBASE
29 
30 #include <thread>
31 #include <mutex>
32 #include "CoreLibraryDllGlobal.h"
34 
36 namespace core_lib
37 {
39 namespace threads
40 {
41 
49 class CORE_LIBRARY_DLL_SHARED_API ThreadBase
50 {
51 public:
53  ThreadBase(const ThreadBase&) = delete;
55  ThreadBase& operator=(const ThreadBase&) = delete;
57  ThreadBase(ThreadBase&&) = default;
59  ThreadBase& operator=(ThreadBase&&) = default;
61  virtual ~ThreadBase() = default;
66  bool IsStarted() const;
74  bool Start();
82  bool Stop();
90  std::thread::id ThreadID() const;
99  std::thread::native_handle_type NativeHandle() const;
100 
101 protected:
103  ThreadBase() = default;
108  void SetTerminating(bool terminating = true);
113  bool IsTerminating() const;
122  void SleepForTime(unsigned int milliSecs) const;
134  virtual void ThreadIteration() NO_EXCEPT_ = 0;
144  virtual void ProcessTerminationConditions() NO_EXCEPT_;
145 
146 private:
152  void SetThreadIdAndNativeHandle(const std::thread::id& threadId,
153  const std::thread::native_handle_type& nativeHandle);
158  void SetStarted(bool started = true);
166  void Run();
167 
168 private:
170  mutable std::mutex m_mutex;
172  bool m_started{false};
174  bool m_terminating{false};
176  std::thread::id m_threadId{};
178  std::thread::native_handle_type m_nativeHandle{};
180  std::thread m_thread{};
181 };
182 
183 } // namespace threads
184 } // namespace core_lib
185 
186 #endif // THREADBASE
std::mutex m_mutex
Access mutex to protect private data.
Definition: ThreadBase.h:170
Thread base class.
Definition: ThreadBase.h:49
The core_lib namespace.
Definition: AsioDefines.h:59
File containing declaration of DLL import/export control defines.
File containing platform specific definitions.
#define NO_EXCEPT_
NO_EXCEPT_ definition mapping to noexcept.
Definition: PlatformDefines.h:57