threadable.hpp
Go to the documentation of this file.
1 
11 /*****************************************************************************
12 ** Ifdefs
13 *****************************************************************************/
14 
15 #ifndef ECL_THREADS_THREADABLE_POS_HPP_
16 #define ECL_THREADS_THREADABLE_POS_HPP_
17 
18 /*****************************************************************************
19 ** Platform Check
20 *****************************************************************************/
21 
22 #include <ecl/config/ecl.hpp>
23 #if defined(ECL_IS_POSIX)
24 
25 /*****************************************************************************
26 ** Includes
27 *****************************************************************************/
28 
29 #include <ecl/utilities/parameter.hpp>
30 #include <ecl/config/macros.hpp>
31 #include "thread.hpp"
32 #include "priority.hpp"
33 
34 /*****************************************************************************
35 ** Namespaces
36 *****************************************************************************/
37 
38 namespace ecl {
39 
40 /*****************************************************************************
41 ** Interface [Threadable]
42 *****************************************************************************/
66 class ECL_PUBLIC Threadable {
67 public:
68  virtual ~Threadable() {}
69 
79  bool start(const Priority &priority = DefaultPriority) {
80  if ( isRunning() ) { return false; }
81  isRunning(true); // This is not ideal, as it's not perfectly atomic (i.e. someone can query isRunning before this gets set).
82  Thread thread(&Threadable::executeThreadFunction,*this, priority);
83  return true;
84  }
90  Parameter<bool> isRunning;
91 
92 protected:
93  Threadable() : isRunning(false) {}
94 
95  virtual void runnable() = 0;
96 
97 private:
98  void executeThreadFunction() {
99  runnable();
100  isRunning(false);
101  }
102 };
103 
104 } // namespace ecl
105 
106 #endif /* ECL_IS_POSIX */
107 #endif /* ECL_THREADS_THREADABLE_POS_HPP_ */
DefaultPriority
Pre-processed macro definitions that define the target platform.
#define ECL_PUBLIC
Priority


xbot_driver
Author(s): Roc, wangpeng@droid.ac.cn
autogenerated on Sat Oct 10 2020 03:27:38