ONPOSIX  2.0
 All Classes Namespaces Files Functions Variables Enumerator Friends Macros Pages
AbstractThread.hpp
Go to the documentation of this file.
1 /*
2  * AbstractThread.hpp
3  *
4  * Copyright (C) 2012 Evidence Srl - www.evidence.eu.com
5  *
6  * This library is free software; you can redistribute it and/or
7  * modify it under the terms of the GNU Library General Public
8  * License as published by the Free Software Foundation; either
9  * version 2 of the License, or (at your option) any later version.
10  *
11  * This library is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14  * Library General Public License for more details.
15  *
16  * You should have received a copy of the GNU Library General Public
17  * License along with this library; if not, write to the Free Software
18  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
19  */
20 
21 #ifndef ABSTRACTTHREAD_HPP_
22 #define ABSTRACTTHREAD_HPP_
23 
24 #ifndef _GNU_SOURCE
25 #define _GNU_SOURCE
26 #endif
27 #include <pthread.h>
28 #include <features.h>
29 
30 #include <vector>
31 
32 // Uncomment to enable Linux-specific methods:
33 #define ONPOSIX_LINUX_SPECIFIC
34 
35 namespace onposix {
36 
37 /**
38  * \brief Abstract for thread implementation.
39  *
40  * Base abstract non copyable class for thread implementation. Derived classes
41  * must implement the pure virtual function run() to define their specific
42  * behavior.
43  * This class makes use of POSIX threads (pthreads).
44  * The thread starts stopped. To start it, the start() method must be
45  * explicitly called.
46  *
47  * Example of usage:
48  * \code
49  * class MyThread: public AbstractThread {
50  * // Put thread arguments here
51  * public:
52  * MyThread() {
53  * // Initialize arguments here
54  * }
55  * void run() {
56  * // Put thread code here
57  * // Access arguments as normal attributes
58  * }
59  * };
60  *
61  * int main ()
62  * {
63  * MyThread t;
64  * t.start();
65  * t.waitForTermination();
66  * }
67  * \endcode
68  *
69  * In case a return value must be passed from the finished thread to the
70  * other thread, this can be achieved as follows:
71  *
72  * \code
73  * class MyThread: public AbstractThread {
74  * int returnValue_;
75  * public:
76  * MyThread(): returnValue_(0) {
77  * // Initialize other arguments here
78  * }
79  * void run() {
80  * // Put thread code here
81  * returnValue_ = ...;
82  * }
83  * inline int getReturnValue() {
84  * return returnValue_;
85  * }
86  * };
87  *
88  * int main ()
89  * {
90  * MyThread t;
91  * t.start();
92  * t.waitForTermination();
93  * // Attention: get return arguments only after the thread has
94  * // terminated the execution, to avoid race conditions!
95  * int ret = t.getReturnValue();
96  * }
97  * \endcode
98  */
100 
101  static void* Execute(void* param);
102 
103  /**
104  * \brief If the thread is running
105  */
107 
110 
111 protected:
112 
113  /**
114  * \brief Function to know if the thread has finished computation.
115  *
116  * This function can be used in the subclasses to check if a request
117  * for termination has been made; if so, the thread is terminated.
118  */
119  static void checkTermination() {
120  pthread_testcancel();
121  }
122 
123  /**
124  * \brief This function must be reimplemented in a derived class
125  * to define the specific function run by the thread.
126  */
127  virtual void run() = 0;
128 
129  pthread_t handle_;
130 
131 public:
132 
133  AbstractThread();
134  virtual ~AbstractThread();
135  bool start();
136  bool stop();
137  bool waitForTermination();
138  static bool blockSignal (int sig);
139  static bool unblockSignal (int sig);
140  bool sendSignal(int sig);
141  static bool setSignalHandler(int sig, void (*handler) (int));
142  bool setSchedParam(int policy, int priority);
143  bool getSchedParam(int* policy, int* priority);
144 
145 #if defined(ONPOSIX_LINUX_SPECIFIC) && defined(__GLIBC__) && \
146  ((__GLIBC__ > 2) || ((__GLIBC__ == 2) && (__GLIBC_MINOR__ > 3)))
147  // Functions to get/set affinity are available only from glibc 2.4
148  void setAffinity(const std::vector<bool>& s);
149  void getAffinity(std::vector<bool>* v);
150 #endif /* ONPOSIX_LINUX_SPECIFIC && GLIBC */
151 };
152 
153 } /* onposix */
154 
155 #endif /* ABSTRACTTHREAD_HPP_ */