Fire Core  8.0.0.alpha
GIS framework per tutti gli usi
 Tutto Classi Namespace Funzioni Variabili Ridefinizioni di tipo (typedef) Tipi enumerati (enum) Valori del tipo enumerato Friend
Thread.hpp
1 // Copyright 2015 GEOSYSTEMS SRL
2 // All Rights Reserved.
3 
4 #ifndef FIRE_GEAR_THREADING_THREAD_HPP_INCLUDED
5 #define FIRE_GEAR_THREADING_THREAD_HPP_INCLUDED
6 
7 #include "fire/export.hpp"
8 
9 #include <boost/smart_ptr/shared_ptr.hpp>
10 #include <boost/thread.hpp>
11 
12 #include <exception>
13 #include <memory>
14 #include <cstddef>
15 #include <string>
16 
17 #include "fire/Object.hpp"
18 #include "fire/gear/threading/Runnable.hpp"
19 
24 namespace fire {
29 namespace gear {
34 namespace threading {
39 class FIRE_ENGINE_DLL Thread : public fire::Object {
40  private:
45  class Callable {
46  private:
51  boost::shared_ptr< fire::gear::threading::Runnable > runnable;
52  public:
60  explicit Callable(boost::shared_ptr< fire::gear::threading::Runnable > runnable) {
61  try {
62  this->runnable = runnable;
63  } catch (const std::exception& exception) {
64  throw exception;
65  }
66  }
71  virtual ~Callable() {
72  try {
73  } catch (const std::exception& exception) {
74  throw exception;
75  }
76  }
81  void operator()() {
82  try {
83  this->runnable->run();
84  } catch (const std::exception& exception) {
85  throw exception;
86  }
87  }
88  };
93  class Guard {
94  private:
99  boost::shared_ptr< boost::thread > thread;
100  public:
108  explicit Guard(boost::shared_ptr< boost::thread > thread) {
109  try {
110  this->thread = thread;
111  } catch (const std::exception& exception) {
112  throw exception;
113  }
114  }
119  virtual ~Guard() {
120  try {
121  this->thread->join();
122  } catch (const std::exception& exception) {
123  throw exception;
124  }
125  }
126  };
127 
128  private:
133  boost::shared_ptr< fire::gear::threading::Runnable > runnable;
138  std::auto_ptr< fire::gear::threading::Thread::Callable > callable;
143  boost::shared_ptr< boost::thread > thread;
148  std::auto_ptr< fire::gear::threading::Thread::Guard > guard;
153  std::auto_ptr< boost::this_thread::disable_interruption > interruptionsDisabled;
158  std::auto_ptr< boost::this_thread::restore_interruption > interruptionsEnabled;
159 
160  protected:
165  void disableInterruptions();
170  void enableInterruptions();
178  bool isInterruptable() const;
183  void setInterruptionPoint();
184 
185  public:
193  static unsigned getCoresCount();
201  static unsigned getVirtualCoresCount();
209  static std::string getThreadId();
217  static void sleepFor(std::size_t milliseconds);
222  Thread();
230  explicit Thread(fire::gear::threading::Runnable* runnable);
235  virtual ~Thread();
243  std::string getId() const;
251  void start(bool guarded = false);
256  virtual void run();
261  void detach();
269  bool isJoinable() const;
274  void join() const;
282  void joinFor(std::size_t milliseconds) const;
287  void interrupt();
288 };
289 } // namespace threading
290 } // namespace gear
291 } // namespace fire
292 
293 #endif
Interfaccia per la gestione di un oggetto che descrive un flusso di esecuzione.
Definition: Runnable.hpp:38
Classe per la gestione di un oggetto.
Definition: Object.hpp:29
Classe per la gestione di un flusso di esecuzione.
Definition: Thread.hpp:39