sptk2 logo
SPTK Home Page
Thread.h
1/*
2╔══════════════════════════════════════════════════════════════════════════════╗
3║ SIMPLY POWERFUL TOOLKIT (SPTK) ║
4╟──────────────────────────────────────────────────────────────────────────────╢
5║ copyright © 1999-2022 Alexey Parshin. All rights reserved. ║
6║ email alexeyp@gmail.com ║
7╚══════════════════════════════════════════════════════════════════════════════╝
8┌──────────────────────────────────────────────────────────────────────────────┐
9│ This library is free software; you can redistribute it and/or modify it │
10│ under the terms of the GNU Library General Public License as published by │
11│ the Free Software Foundation; either version 2 of the License, or (at your │
12│ option) any later version. │
13│ │
14│ This library is distributed in the hope that it will be useful, but │
15│ WITHOUT ANY WARRANTY; without even the implied warranty of │
16│ MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Library │
17│ General Public License for more details. │
18│ │
19│ You should have received a copy of the GNU Library General Public License │
20│ along with this library; if not, write to the Free Software Foundation, │
21│ Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA. │
22│ │
23│ Please report all bugs and problems to alexeyp@gmail.com. │
24└──────────────────────────────────────────────────────────────────────────────┘
25*/
26
27#pragma once
28
29#include <sptk5/threads/Locks.h>
30
31#include "Semaphore.h"
32#include <atomic>
33#include <mutex>
34#include <thread>
35
36namespace sptk {
42class ThreadManager;
43
50class SP_EXPORT Thread
51{
52 SharedMutex m_mutex;
53 String m_name;
54 std::shared_ptr<std::thread> m_thread;
55 bool m_terminated {false};
56 Semaphore m_pause;
57 std::shared_ptr<ThreadManager> m_threadManager;
58
62 void threadStart();
63
64public:
68 using Id = std::thread::id;
69
70
76 explicit Thread(const String& name, const std::shared_ptr<ThreadManager>& threadManager = nullptr);
77
81 virtual ~Thread();
82
86 virtual void run();
87
92 bool running() const;
93
97 virtual void threadFunction() = 0;
98
102 virtual void terminate();
103
107 virtual void onThreadExit()
108 {
109 // Implement in derived class, if needed
110 }
111
115 virtual bool terminated();
116
120 virtual void join();
121
125 Id id() const;
126
130 const String& name() const
131 {
132 return m_name;
133 }
134
140 virtual bool sleep_for(std::chrono::milliseconds interval);
141
147 virtual bool sleep_until(const DateTime& timestamp);
148};
149
153using SThread = std::shared_ptr<Thread>;
154
158using UThread = std::unique_ptr<Thread>;
159
163} // namespace sptk
Definition: DateTime.h:86
Generic unnamed semaphore class.
Definition: Semaphore.h:52
Definition: String.h:49
Definition: Thread.h:51
virtual bool sleep_for(std::chrono::milliseconds interval)
bool running() const
Thread(const String &name, const std::shared_ptr< ThreadManager > &threadManager=nullptr)
virtual void join()
virtual bool terminated()
virtual void run()
Id id() const
virtual void onThreadExit()
Definition: Thread.h:107
const String & name() const
Definition: Thread.h:130
virtual void terminate()
virtual bool sleep_until(const DateTime &timestamp)
std::thread::id Id
Definition: Thread.h:68
virtual ~Thread()
virtual void threadFunction()=0
std::unique_ptr< Thread > UThread
Definition: Thread.h:158
std::shared_ptr< Thread > SThread
Definition: Thread.h:153

Fri Oct 14 2022 09:58:32: SPTK 5.4.1