Core Library 1.7.0.0 Library containing core utilities and tools for threading, networking, logging, INI and CSV file management etc.

ThreadGroup.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 THREADGROUP
28 #define THREADGROUP
29
30 #include <thread>
31 #include <mutex>
32 #include <list>
33 #include <vector>
34 #include "CoreLibraryDllGlobal.h"
35 #include "Platform/PlatformDefines.h"
36
38 namespace core_lib
39 {
41 namespace threads
42 {
43
54 class CORE_LIBRARY_DLL_SHARED_API ThreadGroup final
55 {
56 public:
58 ThreadGroup() = default;
60 ~ThreadGroup();
62 ThreadGroup(const ThreadGroup&) = delete;
64 ThreadGroup& operator=(const ThreadGroup&) = delete;
66 ThreadGroup(ThreadGroup&&) = delete;
68 ThreadGroup& operator=(ThreadGroup&&) = delete;
73 bool IsThisThreadIn() const;
79 bool IsThreadIn(std::thread* threadPtr) const;
85 bool IsThreadIn(const std::thread::id& id) const;
91 template <typename F> std::thread* CreateThread(F&& threadfunction)
92 {
93 std::lock_guard<std::mutex> lock{m_mutex};
94 std::unique_ptr<std::thread> newThread{new std::thread(std::forward<F>(threadfunction))};
95 m_threadGroup.push_back(newThread.get());
96 return newThread.release();
97 }
105 void AddThread(std::thread* threadPtr);
110 void RemoveThread(std::thread* threadPtr);
120 std::thread* RemoveThread(const std::thread::id& id);
125 bool JoinAll();
129 size_t Size() const;
133 bool Empty() const;
134
135 private:
137 mutable std::mutex m_mutex;
139 using thread_list = std::list<std::thread*>;
141 using thread_list_iter = thread_list::iterator;
143 thread_list m_threadGroup;
148 bool IsThisThreadInNoMutex() const;
154 bool IsThreadInNoMutex(const std::thread::id& id) const;
159 static void DeleteThread(std::thread* threadPtr);
160 };
161
162 } // namespace threads
163 } // namespace core_lib
164
165 #endif // THREADGROUP
core_lib::threads::ThreadGroup::CreateThread
std::thread * CreateThread(F &&threadfunction)
Create and add thread to group.
Definition: ThreadGroup.h:91
core_lib::threads::ThreadGroup::m_mutex
std::mutex m_mutex
Access mutex for private data.
Definition: ThreadGroup.h:137
core_lib
The core_lib namespace.
Definition: AsioDefines.h:59
core_lib::threads::ThreadGroup
Thread group class.
Definition: ThreadGroup.h:54
CoreLibraryDllGlobal.h
File containing declaration of DLL import/export control defines.
PlatformDefines.h
File containing platform specific definitions.
core_lib::threads::ThreadGroup::m_threadGroup
thread_list m_threadGroup
List containing threads.
Definition: ThreadGroup.h:143
core_lib::threads::ThreadGroup::thread_list_iter
thread_list::iterator thread_list_iter
Typedef for thread list iterator type.
Definition: ThreadGroup.h:141
core_lib::threads::ThreadGroup::thread_list
std::list< std::thread * > thread_list
Typedef for thread list type.
Definition: ThreadGroup.h:139