ddc
ddcThread.h
Go to the documentation of this file.
1 //-*- Mode: C++ -*-
2 //
3 // DDC originally by Alexey Sokirko
4 // This file by Bryan Jurish, adapted from https://vichargrave.github.io/articles/2012-12/java-style-thread-class-in-cpp
5 //
6 // This file is part of DDC.
7 //
8 // DDC is free software: you can redistribute it and/or modify
9 // it under the terms of the GNU Lesser General Public License as published by
10 // the Free Software Foundation, either version 3 of the License, or
11 // (at your option) any later version.
12 //
13 // DDC is distributed in the hope that it will be useful,
14 // but WITHOUT ANY WARRANTY; without even the implied warranty of
15 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 // GNU Lesser General Public License for more details.
17 //
18 // You should have received a copy of the GNU Lesser General Public License
19 // along with DDC. If not, see <http://www.gnu.org/licenses/>.
20 //
21 #ifndef DDC_THREAD_H
22 #define DDC_THREAD_H
23 
24 #include "ddcConfig.h"
25 #include <pthread.h>
26 #include <string>
27 
28 //======================================================================
31 {
32 public:
34  mutable pthread_mutex_t m_Mutex;
35 
36 public:
39  { pthread_mutex_init(&m_Mutex,NULL); };
40 
42  virtual ~ddcLockable()
43  { pthread_mutex_destroy(&m_Mutex); };
44 
45 
46 public:
48  inline int lock() const
49  { return pthread_mutex_lock(&m_Mutex); };
50 
52  inline int unlock() const
53  { return pthread_mutex_unlock(&m_Mutex); };
54 };
55 
56 
57 //======================================================================
59 class ddcThread
60 {
61 protected:
62  //--------------------------------------------------------------
64 
65  pthread_t m_tid;
67 
69  pthread_attr_t* m_attr;
70 
72  int m_running;
73 
77 
78 public:
79  //--------------------------------------------------------------
81 
82 
85  : m_tid(0), m_attr(NULL), m_running(0), m_detached(0)
86  {};
87 
92  virtual ~ddcThread();
94 
95 public:
96  //--------------------------------------------------------------
98 
99  inline pthread_t self() const
101  { return m_tid; };
102 
104  virtual std::string toString() const;
105 
107  inline pthread_attr_t* attr() const
108  { return m_attr; };
109 
111  inline int running() const
112  { return m_running; };
113 
115  inline int detached() const
116  { return m_detached; };
118 
119 public:
120  //--------------------------------------------------------------
122 
123 
127  virtual void init()
128  {};
129 
133  virtual int start();
134 
140  virtual int join();
141 
150  virtual int detach(bool force=false);
151 
156  virtual void* run() = 0;
158 
159 };
160 
161 #endif /* defined(DDC_THREAD_H) */
162 
163 /*--- emacs style variables ---
164  * Local Variables:
165  * mode: C++
166  * c-file-style: "ellemtel"
167  * c-basic-offset: 4
168  * tab-width: 8
169  * indent-tabs-mode: nil
170  * End:
171  */
generic lockable object wrapper class
Definition: ddcThread.h:30
int m_detached
is this thread currently detached?
Definition: ddcThread.h:75
int unlock() const
Definition: ddcThread.h:52
virtual ~ddcLockable()
Definition: ddcThread.h:42
virtual void init()
Definition: ddcThread.h:127
generic pthread wrapper class
Definition: ddcThread.h:59
pthread_mutex_t m_Mutex
Definition: ddcThread.h:34
int detached() const
get value of m_detached flag
Definition: ddcThread.h:115
int m_running
is this thread is currently running?
Definition: ddcThread.h:72
pthread_attr_t * m_attr
underlying thread attributes (NULL by default)
Definition: ddcThread.h:69
ddcThread()
Definition: ddcThread.h:84
pthread_attr_t * attr() const
get current thread attributes (for use by child classes)
Definition: ddcThread.h:107
ddcLockable()
Definition: ddcThread.h:38
int lock() const
Definition: ddcThread.h:48
int running() const
get value of m_running flag
Definition: ddcThread.h:111