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 
28 #include <ArmarXCore/interface/core/ThreadingIceBase.h>
30 
31 #include <IceUtil/Thread.h>
32 
33 #include <functional>
34 #include <memory>
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]()
64  {
65  return (parent->*runningFn)();
66  };
67  }
68 
69  /**
70  * Destructor stops the thread and waits for completion.
71  */
72  ~RunningTaskBase() override;
73 
74  void setName(const std::string& name);
75 
76  void setThreadList(ThreadListPtr threadList);
77 
78  /**
79  * Starts the thread. Only has effect if the task has not been started. After completion of the task, it cannot be started again.
80  */
81  void start();
82 
83  /**
84  * Stops the thread. Only has effect if the task has been started.
85  *
86  * @param waitForJoin wait for the thread to terminate and join with the process.
87  */
88  void stop(bool waitForJoin = true);
89 
90  /**
91  * @brief Wait for the RunningTask to finish *without* telling it to finish.
92  */
93  void join();
94 
95  /**
96  * Retrieve running state of the thread. The task is running when it is started until the thread method joints.
97  *
98  * @return running state
99  */
100  bool isRunning() const;
101 
102  /**
103  * Retrieve finished state of the thread. The task is finished once the thread has joined.
104  *
105  * @return finished state
106  */
107  bool isFinished() const;
108 
109  /**
110  * wait blocking for thread to be finished. The task is finished once the thread has joined.
111  * @param timeoutMS timeout in milliseconds. If -1 there is no timeout.
112  * @return returns true if the task finished in the given time interval
113  */
114  bool waitForFinished(int timeoutMS = -1);
115 
116  /**
117  * Retrieve whether stop() has been called. Has to be handled in thread function implementation for proper exit.
118  *
119  * @return stopped state
120  */
121  bool isStopped();
122 
123  /**
124  * Wait blocking for thread until stop() has been called. Can be used for blocking wait for stop in thread function implementation
125  */
126  void waitForStop();
127 
128  std::string getName() const;
129 
130  private:
131  void run() override;
132 
133  struct Impl;
134  std::unique_ptr<Impl> impl;
135  };
136 
137  /**
138  \page ThreadsDoc ArmarX Threads
139  ArmarX provides utility methods in order to include different types of threads. In order to support the inclusion
140  of multiple threads in a single objects, without the neccessity to write additional thread classes the mechanism
141  uses functionoids of class methods.
142  Two types of threads are provided by the ArmarX core: the RunningTask and the PeriodicTask. These can be
143  extended in projects which use the ArmarX core.
144 
145  The API documentation can be found here: \ref Threads
146 
147  \section RunningTasks Using running tasks
148  The running task executes one thread method once. The task can be started only once.
149  \include RunningTask.dox
150 
151  \section PeriodicTasks Using periodic tasks
152  The periodic task executes one thread method repeatedly using the time period specified in the constructor.
153  \include PeriodicTask.dox
154 
155 
156 
157  \defgroup Threads
158  \ingroup core-utility
159 
160  * \class RunningTask
161  * \ingroup Threads
162  */
163  template <class T>
164  class ARMARXCORE_IMPORT_EXPORT RunningTask
165  : public RunningTaskBase
166  {
167  public:
168  /**
169  * Typedef for the thread method. Thread methods need to follow the template
170  * void methodName(void). Parameter passing is not used since methods are members
171  * of the class.
172  */
173  typedef void (T::*method_type)(void);
174 
175  /**
176  * Shared pointer type for convenience.
177  */
179 
180  /**
181  * Constructs a running task within the class parent which calls the \p runningFn in
182  * a new thread.
183  *
184  * @param name of this thread, that describes what it is doing. If string
185  * is empty, it will use the name of the class with RTTI
186  */
187  RunningTask(T* parent, method_type runningFn, const std::string& name = "")
188  : RunningTaskBase(parent, runningFn, name)
189  {
190  }
191  };
192 
193 
194 
195 }
196 
197 
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:187
cxxopts::empty
bool empty(const std::string &s)
Definition: cxxopts.hpp:255
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:67
armarx::RunningTaskBase
Definition: RunningTask.h:41
armarx::GetTypeString
std::string GetTypeString(const std::type_info &tinf, bool withoutNamespaceSpecifier=false)
Definition: GetTypeString.h:36
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:29
ImportExport.h
ARMARXCORE_IMPORT_EXPORT
#define ARMARXCORE_IMPORT_EXPORT
Definition: ImportExport.h:38
T
float T
Definition: UnscentedKalmanFilterTest.cpp:35
armarx
This file offers overloads of toIce() and fromIce() functions for STL container types.
Definition: ArmarXTimeserver.cpp:28