Libthreadar 1.6.0
thread.hpp
Go to the documentation of this file.
1/*********************************************************************/
2// libthreadar - is a library providing several C++ classes to work with threads
3// Copyright (C) 2014-2025 Denis Corbin
4//
5// This file is part of libthreadar
6//
7// libthreadar is free software: you can redistribute it and/or modify
8// it under the terms of the GNU Lesser General Public License as published by
9// the Free Software Foundation, either version 3 of the License, or
10// (at your option) any later version.
11//
12// libhtreadar 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 Lesser General Public License for more details.
16//
17// You should have received a copy of the GNU Lesser General Public License
18// along with libthreadar. If not, see <http://www.gnu.org/licenses/>
19//
20//----
21// to contact the author: dar.linux@free.fr
22/*********************************************************************/
23
24#ifndef LIBTHREADAR_THREAD_HPP
25#define LIBTHREADAR_THREAD_HPP
26
34
35#include "config.h"
36
37 // C system headers
38extern "C"
39{
40#if HAVE_PTHREAD_H
41#include <pthread.h>
42#endif
43#if HAVE_SIGNAL_H
44#include <signal.h>
45#endif
46}
47 // C++ standard headers
48
49
50 // libthreadar headers
51#include "mutex.hpp"
52
53namespace libthreadar
54{
55
57
100
101 class thread
102 {
103 public:
106
108 thread(const thread & ref) = delete;
109 thread(thread && ref) noexcept = default;
110 thread & operator = (const thread & ref) = delete;
111 thread & operator = (thread && ref) noexcept = default;
112
114 virtual ~thread();
115
117
122
124
128 void set_stack_size(unsigned int val);
129
131
134 unsigned int get_stack_size() const { return stack_size; };
135
137
140 virtual void set_signal_mask(const sigset_t & mask) { sigmask = mask; };
141
143 void run();
144
146 bool is_running() const { return running; };
147
149
153 bool is_running(pthread_t & id) const;
154
156 void join() const;
157
163 void kill() const;
164
165
167
172 void cancel();
173
174
175
176 protected:
177
179
182 {
183 public:
184 cancel_except() {};
185 cancel_except(cancel_except &) = default;
186 cancel_except(cancel_except &&) noexcept = default;
187 cancel_except & operator = (cancel_except &) = default;
188 cancel_except & operator = (cancel_except &&) noexcept = default;
189 ~cancel_except() = default;
190 };
191
193
201 virtual void inherited_run() = 0;
202
204
213
242
251 virtual void inherited_cancel() {};
252
253 private:
254 mutable mutex field_control;
255 bool running;
256 pthread_t tid;
257 bool joignable;
258 mutable bool do_cancel;
259 sigset_t sigmask;
260 unsigned int stack_size;
261 char* stack;
262
263
264 void clear_stack();
265
266 // static members
267
268 static void *run_obj(void *obj); //< called by pthread_create to spawn a new thread
269 };
270
274
275} // end of namespace
276
277#endif
Wrapper around the Posix pthread_mutex_t C objects.
Definition: mutex.hpp:57
exception used to trigger thread cancellation
Definition: thread.hpp:182
Class thread is a pure virtual class, that implements thread creation and operations.
Definition: thread.hpp:102
bool is_running() const
checks whether a separated thread is running the inherited_run() method of this object
Definition: thread.hpp:146
void reset_stack_size()
reset the stack size to the system default value
void cancellation_checkpoint() const
available withing the inherited_run() method to eventually trigger thread cancellation
virtual void set_signal_mask(const sigset_t &mask)
set signal mask of the thread spawn by run()
Definition: thread.hpp:140
void run()
launch the current object routing in a separated thread
virtual void inherited_cancel()
Definition: thread.hpp:251
unsigned int get_stack_size() const
get the current stack size value
Definition: thread.hpp:134
void kill() const
void cancel()
the caller send a cancellation request to this object's running thread if any
virtual void inherited_run()=0
action to be performed in the separated thread (implementation is expected in inherited classes)
bool is_running(pthread_t &id) const
checks whether the object is running in a separated thread
thread(const thread &ref)=delete
copy constructor and assignment operator are disabled
void set_stack_size(unsigned int val)
set the stack size to non default value
virtual ~thread()
destructor
thread()
constructor
void join() const
the caller will be suspended until the current object's thread ends
defines the mutex C++ class
This is the only namespace used in libthreadar and all symbols provided by libthreadar are member of ...
Definition: barrier.hpp:46