RSC  0.16.0
ThreadedTaskExecutor.cpp
Go to the documentation of this file.
1 /* ============================================================
2  *
3  * This file is a part of the RSC project.
4  *
5  * Copyright (C) 2011 by Johannes Wienke <jwienke at techfak dot uni-bielefeld dot de>
6  *
7  * This file may be licensed under the terms of the
8  * GNU Lesser General Public License Version 3 (the ``LGPL''),
9  * or (at your option) any later version.
10  *
11  * Software distributed under the License is distributed
12  * on an ``AS IS'' basis, WITHOUT WARRANTY OF ANY KIND, either
13  * express or implied. See the LGPL for the specific language
14  * governing rights and limitations.
15  *
16  * You should have received a copy of the LGPL along with this
17  * program. If not, go to http://www.gnu.org/licenses/lgpl.html
18  * or write to the Free Software Foundation, Inc.,
19  * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
20  *
21  * The development of this software was supported by:
22  * CoR-Lab, Research Institute for Cognition and Robotics
23  * Bielefeld University
24  *
25  * ============================================================ */
26 
27 #include "ThreadedTaskExecutor.h"
28 
29 #include <stdexcept>
30 
31 using namespace std;
32 
33 namespace rsc {
34 namespace threading {
35 
36 ThreadedTaskExecutor::ThreadedTaskExecutor() {
37 }
38 
39 ThreadedTaskExecutor::~ThreadedTaskExecutor() {
40 }
41 
42 void ThreadedTaskExecutor::schedule(TaskPtr t) {
43  this->schedule(t, 0);
44 }
45 
46 void ThreadedTaskExecutor::schedule(TaskPtr t,
47  const boost::uint64_t& delayMus) {
48  if (t->isCancelRequested()) {
49  throw std::invalid_argument("Task already canceled.");
50  }
51  boost::thread taskThread(
52  boost::bind(ThreadedTaskExecutor::executeTask, t, delayMus));
53  // detach the thread because all further operations can be done on the
54  // task object and this executor does not have to care about the thread
55  taskThread.detach();
56 }
57 
58 void ThreadedTaskExecutor::executeTask(TaskPtr task,
59  const boost::uint64_t& delayMus) {
60  if (delayMus > 0) {
61  boost::this_thread::sleep(boost::posix_time::microseconds(delayMus));
62  }
63  task->run();
64 }
65 
66 }
67 }
STL namespace.
boost::shared_ptr< Task > TaskPtr
Definition: Task.h:103