Core Library  1.7.0.0
Library containing core utilities and tools for threading, networking, logging, INI and CSV file management etc.
JoinThreads.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 JOINTHREADS
28 #define JOINTHREADS
29 
30 #include <thread>
31 
33 namespace core_lib
34 {
36 namespace threads
37 {
38 
48 template <template <class, class> class C> class JoinThreads final
49 {
50 public:
52  using container_type = C<std::thread, std::allocator<std::thread>>;
57  explicit JoinThreads(container_type& threads)
58  : m_threads(threads)
59  {
60  }
62  JoinThreads(const JoinThreads&) = delete;
64  JoinThreads& operator=(const JoinThreads&) = delete;
66  JoinThreads(JoinThreads&&) = delete;
68  JoinThreads& operator=(JoinThreads&&) = delete;
71  {
72  for (auto& t : m_threads)
73  {
74  if (t.joinable())
75  {
76  t.join();
77  }
78  }
79  }
80 
81 private:
84 };
85 
95 template <template <class, class> class C> class JoinThreadsP final
96 {
97 public:
99  using container_type = C<std::thread*, std::allocator<std::thread*>>;
104  explicit JoinThreadsP(container_type& threads)
105  : m_threads(threads)
106  {
107  }
109  JoinThreadsP(const JoinThreadsP&) = delete;
111  JoinThreadsP& operator=(const JoinThreadsP&) = delete;
113  JoinThreadsP(JoinThreadsP&&) = delete;
115  JoinThreadsP& operator=(JoinThreadsP&&) = delete;
118  {
119  for (auto& t : m_threads)
120  {
121  if (t && t->joinable())
122  {
123  t->join();
124  }
125  }
126  }
127 
128 private:
131 };
132 
133 } // namespace threads
134 } // namespace core_lib
135 
136 #endif // JOINTHREADS
JoinThreadsP(container_type &threads)
Initialisation constructor.
Definition: JoinThreads.h:104
C< std::thread, std::allocator< std::thread > > container_type
typedef for container type
Definition: JoinThreads.h:52
~JoinThreads()
Destructor- joins the threads.
Definition: JoinThreads.h:70
Class to manage joining multiple threads held as pointers in an STL container.
Definition: JoinThreads.h:95
container_type & m_threads
Container of threads.
Definition: JoinThreads.h:130
The core_lib namespace.
Definition: AsioDefines.h:59
Class to manage joining multiple threads held as objects in an STL container.
Definition: JoinThreads.h:48
JoinThreads(container_type &threads)
Initialisation constructor.
Definition: JoinThreads.h:57
~JoinThreadsP()
Destructor- joins the threads.
Definition: JoinThreads.h:117
C< std::thread *, std::allocator< std::thread * > > container_type
typedef for container type
Definition: JoinThreads.h:99
container_type & m_threads
Container of threads.
Definition: JoinThreads.h:83
JoinThreads & operator=(const JoinThreads &)=delete
Copy assignment operator deleted.