RunningTask.h
Go to the documentation of this file.
1 /*
2 * This file is part of ArmarX.
3 *
4 * Copyright (C) 2011-2016, High Performance Humanoid Technologies (H2T), Karlsruhe Institute of Technology (KIT), all rights reserved.
5 *
6 * ArmarX is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License version 2 as
8 * published by the Free Software Foundation.
9 *
10 * ArmarX is distributed in the hope that it will be useful, but
11 * WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
14 *
15 * You should have received a copy of the GNU General Public License
16 * along with this program. If not, see <http://www.gnu.org/licenses/>.
17 *
18 * @package ArmarXCore::core
19 * @author Kai Welke (welke at kit dot edu)
20 * @date 2012
21 * @copyright http://www.gnu.org/licenses/gpl-2.0.txt
22 * GNU General Public License
23 */
24 
25 #pragma once
26 
27 #include <functional>
28 #include <memory>
29 
30 #include <IceUtil/Thread.h>
31 
33 #include <ArmarXCore/interface/core/ThreadingIceBase.h>
35 
36 namespace armarx
37 {
38  class ThreadList;
40 
42  virtual public IceUtil::Thread,
43  virtual protected RunningTaskIceBase
44  {
45  public:
46  using CallbackT = std::function<void()>;
47 
49 
50  RunningTaskBase(std::string const& name);
51 
52  /**
53  * Constructs a running task within the class parent which calls the \p runningFn in
54  * a new thread.
55  *
56  * @param name of this thread, that describes what it is doing. If string
57  * is empty, it will use the name of the class with RTTI
58  */
59  template <typename T>
60  RunningTaskBase(T* parent, void (T::*runningFn)(), const std::string& name = "") :
61  RunningTaskBase(name.empty() ? GetTypeString<T>() : name)
62  {
63  callback = [parent, runningFn]() { return (parent->*runningFn)(); };
64  }
65 
66  /**
67  * Destructor stops the thread and waits for completion.
68  */
69  ~RunningTaskBase() override;
70 
71  void setName(const std::string& name);
72 
73  void setThreadList(ThreadListPtr threadList);
74 
75  /**
76  * Starts the thread. Only has effect if the task has not been started. After completion of the task, it cannot be started again.
77  */
78  void start();
79 
80  /**
81  * Stops the thread. Only has effect if the task has been started.
82  *
83  * @param waitForJoin wait for the thread to terminate and join with the process.
84  */
85  void stop(bool waitForJoin = true);
86 
87  /**
88  * @brief Wait for the RunningTask to finish *without* telling it to finish.
89  */
90  void join();
91 
92  /**
93  * Retrieve running state of the thread. The task is running when it is started until the thread method joints.
94  *
95  * @return running state
96  */
97  bool isRunning() const;
98 
99  /**
100  * Retrieve finished state of the thread. The task is finished once the thread has joined.
101  *
102  * @return finished state
103  */
104  bool isFinished() const;
105 
106  /**
107  * wait blocking for thread to be finished. The task is finished once the thread has joined.
108  * @param timeoutMS timeout in milliseconds. If -1 there is no timeout.
109  * @return returns true if the task finished in the given time interval
110  */
111  bool waitForFinished(int timeoutMS = -1);
112 
113  /**
114  * Retrieve whether stop() has been called. Has to be handled in thread function implementation for proper exit.
115  *
116  * @return stopped state
117  */
118  bool isStopped();
119 
120  /**
121  * Wait blocking for thread until stop() has been called. Can be used for blocking wait for stop in thread function implementation
122  */
123  void waitForStop();
124 
125  std::string getName() const;
126 
127  private:
128  void run() override;
129 
130  struct Impl;
131  std::unique_ptr<Impl> impl;
132  };
133 
134  /**
135  \page ThreadsDoc ArmarX Threads
136  ArmarX provides utility methods in order to include different types of threads. In order to support the inclusion
137  of multiple threads in a single objects, without the neccessity to write additional thread classes the mechanism
138  uses functionoids of class methods.
139  Two types of threads are provided by the ArmarX core: the RunningTask and the PeriodicTask. These can be
140  extended in projects which use the ArmarX core.
141 
142  The API documentation can be found here: \ref Threads
143 
144  \section RunningTasks Using running tasks
145  The running task executes one thread method once. The task can be started only once.
146  \include RunningTask.dox
147 
148  \section PeriodicTasks Using periodic tasks
149  The periodic task executes one thread method repeatedly using the time period specified in the constructor.
150  \include PeriodicTask.dox
151 
152 
153 
154  \defgroup Threads
155  \ingroup core-utility
156 
157  * \class RunningTask
158  * \ingroup Threads
159  */
160  template <class T>
161  class ARMARXCORE_IMPORT_EXPORT RunningTask : public RunningTaskBase
162  {
163  public:
164  /**
165  * Typedef for the thread method. Thread methods need to follow the template
166  * void methodName(void). Parameter passing is not used since methods are members
167  * of the class.
168  */
169  typedef void (T::*method_type)(void);
170 
171  /**
172  * Shared pointer type for convenience.
173  */
175 
176  /**
177  * Constructs a running task within the class parent which calls the \p runningFn in
178  * a new thread.
179  *
180  * @param name of this thread, that describes what it is doing. If string
181  * is empty, it will use the name of the class with RTTI
182  */
183  RunningTask(T* parent, method_type runningFn, const std::string& name = "") :
184  RunningTaskBase(parent, runningFn, name)
185  {
186  }
187  };
188 
189 
190 } // namespace armarx
GetTypeString.h
armarx::RunningTask::RunningTask
RunningTask(T *parent, method_type runningFn, const std::string &name="")
Constructs a running task within the class parent which calls the runningFn in a new thread.
Definition: RunningTask.h:183
cxxopts::empty
bool empty(const std::string &s)
Definition: cxxopts.hpp:234
armarx::RunningTaskBase::callback
CallbackT callback
Definition: RunningTask.h:48
IceInternal::Handle
Definition: forward_declarations.h:8
armarx::RunningTaskBase::CallbackT
std::function< void()> CallbackT
Definition: RunningTask.h:46
armarx::TaskStatus::isRunning
bool isRunning(Status status)
Returns whether the given task status describes a state where a path is planned.
Definition: PlanningUtil.cpp:69
armarx::RunningTaskBase
Definition: RunningTask.h:41
armarx::GetTypeString
std::string GetTypeString(const std::type_info &tinf, bool withoutNamespaceSpecifier=false)
Definition: GetTypeString.h:38
armarx::RunningTaskBase::RunningTaskBase
RunningTaskBase(T *parent, void(T::*runningFn)(), const std::string &name="")
Constructs a running task within the class parent which calls the runningFn in a new thread.
Definition: RunningTask.h:60
IceUtil::Handle
Definition: forward_declarations.h:30
ImportExport.h
ARMARXCORE_IMPORT_EXPORT
#define ARMARXCORE_IMPORT_EXPORT
Definition: ImportExport.h:38
T
float T
Definition: UnscentedKalmanFilterTest.cpp:38
armarx
This file offers overloads of toIce() and fromIce() functions for STL container types.
Definition: ArmarXTimeserver.cpp:27