Runnable.h
Go to the documentation of this file.
1 #ifndef _NOVADAQ_RUNNABLE_H
2 #define _NOVADAQ_RUNNABLE_H
3 
4 #include <unistd.h>
5 
6 namespace novadaq {
7 
8 /**
9  * Interface for objects that can be run in separate threads using the
10  * Boost Thread library.
11  *
12  * Sample usage:
13  * <code>
14  * RunnableDescendant myRunnable;
15  * boost::thread
16  * myThread(boost::bind(&novadaq::Runnable::run, &myRunnable));
17  * </code>
18  *
19  * @author Kurt Biery
20  * @author Steve Foulkes
21  * @version $Revision: 1.4.34.1 $ $Date: 2019/09/27 00:07:20 $
22  */
23 
24 // Implementation note: Classes that implement the Runnable interface
25 // can make use of the stop() and isRunning() methods *only* if the derived
26 // classes are careful to keep the relevant attribute values current and
27 // test against them where appropriate.
28 //
29 // In particular, a thorough implementation of the Runnable interface would
30 // include the following features:
31 // - assignment of _running to "true" at the beginning of the run() method
32 // - assignment of _running to "false" at the end of the run() method
33 // - periodic testing of the _stopRequested attributed within the run()
34 // method to see if it should clean up and return
35 
36 class Runnable {
37  public:
38  /**
39  * Default constructor.
40  */
42  _running = false;
43  _stopRequested = false;
44  _hasCompleted = false;
45  }
46 
47  /**
48  * Virtual destructor (to ensure that derived class destructors
49  * are called).
50  */
51  virtual ~Runnable() {}
52 
53  /**
54  * Contains the operations that are to be executed in a separate thread.
55  */
56  virtual void run() = 0;
57 
58  /**
59  * Stops the execution of the Runnable object.
60  */
61  virtual void stop() {
62  if (_hasCompleted) {return;}
63  _stopRequested = true;
64  }
65 
66  /**
67  * Tests if the Runnable object is running.
68  *
69  * @return true if the object is running, false otherwise.
70  */
71  virtual bool isRunning() const {
72  return _running;
73  }
74 
75  /**
76  * Tests if the Runnable object has completed its running.
77  *
78  * @return true if the object is done, false otherwise.
79  */
80  virtual bool isDone() const {
81  return _hasCompleted;
82  }
83 
84  /**
85  * Sleeps for the specified amount of time, but periodically
86  * checks if a stop has been requested, and returns if so.
87  */
88  virtual void watchfulSleep(int microseconds,
89  int numberOfIntervals = 10) {
90  if (numberOfIntervals < 1) {numberOfIntervals = 1;}
91  int intervalTime = microseconds / numberOfIntervals;
92  if (intervalTime < 1) {intervalTime = 1;}
93  for (int idx = 0; idx < numberOfIntervals; ++idx) {
94  if (_stopRequested) {break;}
95  usleep(intervalTime);
96  }
97  }
98 
99  protected:
100 
101  /**
102  * Tracks whether the Runnable object is running or not
103  * (this can be before it has started or after it has finished)
104  */
105  bool _running;
106 
107  /**
108  * Indicates that a request has been made to stop running.
109  */
111 
112  /**
113  * Tracks whether the Runnable object has completed its running.
114  */
116 
117 };
118 
119 } // end of namespace novadaq
120 
121 #endif
virtual void run()=0
virtual bool isDone() const
Definition: Runnable.h:80
virtual void watchfulSleep(int microseconds, int numberOfIntervals=10)
Definition: Runnable.h:88
virtual bool isRunning() const
Definition: Runnable.h:71
virtual ~Runnable()
Definition: Runnable.h:51
virtual void stop()
Definition: Runnable.h:61