sptk2 logo
SPTK Home Page
ThreadManager.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/SynchronizedQueue.h>
30#include <sptk5/threads/Thread.h>
31
32namespace sptk {
33
35{
36public:
37 explicit ThreadManager(const String& name);
38
40
41 void start() const;
42
43 void stop();
44
45 void registerThread(Thread* thread);
46
47 void destroyThread(Thread* thread);
48
49 size_t threadCount() const;
50
51 bool running() const;
52
53private:
54 class Joiner
55 : public Thread
56 {
57 public:
58 explicit Joiner(const String& name);
59
60 void push(const SThread& thread);
61
62 void stop();
63
64 protected:
65 void threadFunction() override;
66
67 void joinTerminatedThreads(std::chrono::milliseconds timeout);
68
69 private:
70 SynchronizedQueue<SThread> m_terminatedThreads;
71 };
72
73 mutable std::mutex m_mutex;
74 std::map<Thread*, SThread> m_runningThreads;
75 std::shared_ptr<Joiner> m_joiner;
76
77 void terminateRunningThreads();
78};
79
80using SThreadManager = std::shared_ptr<ThreadManager>;
81
82} // namespace sptk
Definition: String.h:49
Definition: ThreadManager.h:35
Definition: Thread.h:51
std::shared_ptr< Thread > SThread
Definition: Thread.h:153

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