Libthreadar 1.5.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-2024 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
97
98 class thread
99 {
100 public:
103
105 thread(const thread & ref) = delete;
106 thread(thread && ref) noexcept = default;
107 thread & operator = (const thread & ref) = delete;
108 thread & operator = (thread && ref) noexcept = default;
109
111 virtual ~thread();
112
114
116 void set_signal_mask(const sigset_t & mask) { sigmask = mask; };
117
119 void run();
120
122 bool is_running() const { return running; };
123
125
129 bool is_running(pthread_t & id) const;
130
132 void join() const;
133
139 void kill() const;
140
141
143
146 void cancel();
147
148
149
150 protected:
151
153
156 {
157 public:
158 cancel_except() {};
159 cancel_except(cancel_except &) = default;
160 cancel_except(cancel_except &&) noexcept = default;
161 cancel_except & operator = (cancel_except &) = default;
162 cancel_except & operator = (cancel_except &&) noexcept = default;
163 ~cancel_except() = default;
164 };
165
167
172 virtual void inherited_run() = 0;
173
175
212
220 virtual void inherited_cancel() {};
221
222 private:
223 mutable mutex field_control;
224 bool running;
225 pthread_t tid;
226 bool joignable;
227 mutable bool do_cancel;
228 sigset_t sigmask;
229
230 // static members
231
232 static void *run_obj(void *obj); //< called by pthread_create to spawn a new thread
233 };
234
238
239} // end of namespace
240
241#endif
Wrapper around the Posix pthread_mutex_t C objects.
Definition: mutex.hpp:57
exception used to trigger thread cancellation
Definition: thread.hpp:156
Class thread is a pure virtual class, that implements thread creation and operations.
Definition: thread.hpp:99
bool is_running() const
checks whether a separated thread is running the inherited_run() method of this object
Definition: thread.hpp:122
void cancellation_checkpoint() const
available withing the inherited_run() method to eventually trigger thread cancellation
void run()
launch the current object routing in a separated thread
virtual void inherited_cancel()
Definition: thread.hpp:220
void set_signal_mask(const sigset_t &mask)
set signal mask for this object's when the thread will be run
Definition: thread.hpp:116
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
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
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