Libthreadar 1.4.0
Loading...
Searching...
No Matches
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-2020 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
96
97 class thread
98 {
99 public:
102
104 thread(const thread & ref) = delete;
105 thread(thread && ref) noexcept = default;
106 thread & operator = (const thread & ref) = delete;
107 thread & operator = (thread && ref) noexcept = default;
108
110 virtual ~thread();
111
113
115 void set_signal_mask(const sigset_t & mask) { sigmask = mask; };
116
118 void run();
119
121 bool is_running() const { return running; };
122
124
128 bool is_running(pthread_t & id) const;
129
131 void join() const;
132
134
137 void kill() const;
138
139 protected:
140
142
147 virtual void inherited_run() = 0;
148
150
154
157
158 private:
159 mutex field_control; //< mutex protecting access to object's data
160 bool running; //< whether a thread is running
161 pthread_t tid; //< the thread ID of the running thread if any
162 bool joignable; //< whether exist status of thread has to be retrieved
163 unsigned int cancellable; //< this field is not protected by mutex as it ougth to be modified only by the spawn thread. It allows suspend/resume cancellation requests to be re-entrant (0 = cancellation accepted)
164 sigset_t sigmask; //< signal mask to use for the thread
165
166 // static members
167
168 static void *run_obj(void *obj); //< called by pthread_create to spawn a new thread
169 static void primitive_suspend_cancellation_requests();
170 static void primitive_resume_cancellation_requests();
171 };
172
176
177} // end of namespace
178
179#endif
Wrapper around the Posix pthread_mutex_t C objects.
Definition mutex.hpp:57
Class thread is a pure virtual class, that implements thread creation and operations.
Definition thread.hpp:98
bool is_running() const
checks whether a separated thread is running the inherited_run() method of this object
Definition thread.hpp:121
void run()
launch the current object routing in a separated thread
void set_signal_mask(const sigset_t &mask)
set signal mask for this object's when the thread will be run
Definition thread.hpp:115
void kill() const
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
void suspend_cancellation_requests() const
available for inherited class to avoid thread cancellation to occur in a critical section
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
void resume_cancellation_requests() const
available for inherited class to avoid thread cancellation to occur in a critical section
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