fw4spl
Worker.hxx
1 /* ***** BEGIN LICENSE BLOCK *****
2  * FW4SPL - Copyright (C) IRCAD, 2009-2017.
3  * Distributed under the terms of the GNU Lesser General Public License (LGPL) as
4  * published by the Free Software Foundation.
5  * ****** END LICENSE BLOCK ****** */
6 
7 #ifndef __FWTHREAD_WORKER_HXX__
8 #define __FWTHREAD_WORKER_HXX__
9 
10 #include <fwThread/TaskHandler.hpp>
11 
12 #include <future>
13 
14 namespace fwThread
15 {
16 
17 //------------------------------------------------------------------------------
18 
19 template< typename R, typename TASK >
20 std::shared_future< R > Worker::postTask(TASK f)
21 {
22  std::packaged_task< R() > task( f );
23  std::future< R > ufuture = task.get_future();
24 
25  std::function< void() > ftask = ::fwThread::moveTaskIntoFunction(task);
26 
27  if( ::fwThread::getCurrentThreadId() == this->getThreadId())
28  {
29  ftask();
30  }
31  else
32  {
33  this->post(ftask);
34  }
35 
36  return std::move(ufuture);
37 }
38 
39 } //namespace fwThread
40 
41 #endif // __FWTHREAD_WORKER_HXX__
std::shared_future< R > postTask(CALLABLE f)
Requests invocation of the given callable and returns a shared future.
FWTHREAD_API ThreadIdType getCurrentThreadId()
Returns the current thread id.
Definition: Worker.cpp:14
virtual void post(TaskType handler)=0
Requests invocation of the given task handler and returns immediately.
virtual FWTHREAD_API ThreadIdType getThreadId() const =0
Returns the worker&#39;s thread id.
This namespace fwThread provides few tools to execute asynchronous tasks on different threads...