Libthreadar  1.0.1
 All Classes Namespaces Files Functions Macros
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-2015 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
38 extern "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 
53 namespace libthreadar
54 {
55 
57 
88  class thread
89  {
90  public:
92  thread();
93 
95  virtual ~thread();
96 
98 
100  void set_signal_mask(const sigset_t & mask) { sigmask = mask; };
101 
103  void run();
104 
106  bool is_running() const { return running; };
107 
109 
113  bool is_running(pthread_t & id) const;
114 
116  void join() const;
117 
119 
122  void kill() const;
123 
124  protected:
125 
127 
132  virtual void inherited_run() = 0;
133 
135  void suspend_cancellation_requests() const;
136 
138  void resume_cancellation_requests() const;
139 
140  private:
141  mutex field_control; //< mutex protecting access to object's data
142  bool running; //< whether a thread is running
143  pthread_t tid; //< the thread ID of the running thread if any
144  bool joignable; //< whether exist status of thread has to be retrieved
145  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)
146  sigset_t sigmask; //< signal mask to use for the thread
147 
148 
149  // static members
150 
151  static void *run_obj(void *obj); //< called by pthread_create to spawn a new thread
152  static void primitive_suspend_cancellation_requests();
153  static void primitive_resume_cancellation_requests();
154  };
155 
156 } // end of namespace
157 
158 #endif
void join() const
the caller will be suspended until the current object's thread ends
void set_signal_mask(const sigset_t &mask)
set signal mask for this object's when the thread will be run
Definition: thread.hpp:100
defines the mutex C++ class
void kill() const
the caller send a cancellation request to this object's running thread if any
void suspend_cancellation_requests() const
available for inherited class to avoid thread cancellation to occur in a critical section ...
void run()
launch the current object routing in a separated thread
virtual void inherited_run()=0
action to be performed in the separated thread
virtual ~thread()
destructor
Class thread is a pure virtual class, that implements thread creation and operations.
Definition: thread.hpp:88
void resume_cancellation_requests() const
available for inherited class to avoid thread cancellation to occur in a critical section ...
thread()
constructor
bool is_running() const
checks whether a separated thread is running the inherited_run() method of this object ...
Definition: thread.hpp:106
This is the only namespace used in libthreadar and all symbols provided by libthreadar are member of ...
Definition: barrier.hpp:46
Wrapper around the Posix pthread_mutex_t C objects.
Definition: mutex.hpp:56