ANNA Suite  2020b
Multipurpose development suite for Telco applications
Thread.hpp
Go to the documentation of this file.
1 // ANNA - Anna is Not Nothingness Anymore //
2 // //
3 // (c) Copyright 2005-2015 Eduardo Ramos Testillano & Francisco Ruiz Rayo //
4 // //
5 // See project site at http://redmine.teslayout.com/projects/anna-suite //
6 // See accompanying file LICENSE or copy at http://www.teslayout.com/projects/public/anna.LICENSE //
7 
8 
9 #ifndef anna_core_mt_Thread_hpp
10 #define anna_core_mt_Thread_hpp
11 
12 #include <pthread.h>
13 #include <unistd.h>
14 
16 #include <anna/core/mt/Mutex.hpp>
17 #include <anna/core/Allocator.hpp>
18 
19 namespace anna {
20 
21 class Semaphore;
22 class Runnable;
23 class ThreadManager;
24 
31 class Thread {
32 public:
37  struct Flag {
38  enum { None = 0, Joinable = 1 };
39  };
40 
44  Thread() : a_id((pthread_t) - 1), a_manager(NULL), a_flags(Flag::None) {;}
45 
49  virtual ~Thread();
50 
51  // Accesores
55  pthread_t getId() const { return a_id; }
56 
62  void setFlags(const int flags) { a_flags = flags; }
63 
67  bool isJoinable() const { return (a_flags & Flag::Joinable) != 0; }
68 
74  bool isRunning() const { return (a_id != (pthread_t) - 1); }
75 
76  // Metodos
85  void start(Runnable& runnable) noexcept(false);
86 
91  void testCancel() { pthread_testcancel(); }
92 
116  void join() noexcept(false);
117 
122  virtual std::string asString() const ;
123 
124 private:
125  struct Data {
126  Runnable* runnable;
127  Thread* thread;
128  };
129 
130  pthread_t a_id;
131  Data a_data;
132  ThreadManager* a_manager;
133  int a_flags;
134 
135  Thread(const Thread& other);
136 
137  static void* exec(void* arg);
138 
139  friend class Allocator <Thread>;
140  friend class ThreadManager;
141 };
142 
143 } //namespace anna
144 
145 #endif
146 
Definition: Thread.hpp:31
pthread_t getId() const
Definition: Thread.hpp:55
void join() noexcept(false)
Definition: ThreadManager.hpp:27
Definition: Thread.hpp:37
bool isJoinable() const
Definition: Thread.hpp:67
Thread()
Definition: Thread.hpp:44
void setFlags(const int flags)
Definition: Thread.hpp:62
Definition: Allocator.hpp:19
bool isRunning() const
Definition: Thread.hpp:74
void start(Runnable &runnable) noexcept(false)
Definition: Runnable.hpp:27
void testCancel()
Definition: Thread.hpp:91
Definition: app.hpp:12
virtual std::string asString() const
virtual ~Thread()
Definition: Thread.hpp:38
Definition: Thread.hpp:38