rpa::future< Ftor, Thr > Class Template Reference

Allows the execution of a functor in a thread. More...

#include <synchro.h>

Inheritance diagram for rpa::future< Ftor, Thr >:

rpa::spawn< ThreadTree, Ftor > rpa::thread_stack< Ftor, Thr >::node List of all members.

Public Member Functions

Ftor & functor_ref (void)
 For reduction because the inner functor may have intermediate results.
 ~future ()
 future (Ftor &aFtor, Thr *ptrThr)
 The constructor launches the sub-thread.
const result_typewait_end_const (void)
result_typewait_end (void)
 This is used for reducing and the internal value may be changed.

Private Types

typedef Ftor::result_type result_type

Private Member Functions

 future (const future &)
 No default constructor, not copyable nor assignable.
future operator= (const future &)
void sync_end (void)

Static Private Member Functions

static void * func (void *aThis)

Private Attributes

char _retRawBuf [sizeof(result_type)]
 Raw memory to store a return object, no need of default constructor.
Ftor * _ptrFtor
 Not copy the functor to execute, nor the thread that will execute it.
Thr * _ptrThr
 This value is used as a flag too.

Detailed Description

template<class Ftor, class Thr>
class rpa::future< Ftor, Thr >

Allows the execution of a functor in a thread.

Waits until the thread is finished, and stores its result.


Member Typedef Documentation

template<class Ftor, class Thr>
typedef Ftor::result_type rpa::future< Ftor, Thr >::result_type [private]

Reimplemented in rpa::spawn< ThreadTree, Ftor >, and rpa::thread_stack< Ftor, Thr >::node.


Constructor & Destructor Documentation

template<class Ftor, class Thr>
rpa::future< Ftor, Thr >::future ( const future< Ftor, Thr > &   )  [private]

No default constructor, not copyable nor assignable.

template<class Ftor, class Thr>
rpa::future< Ftor, Thr >::~future (  )  [inline]

Here, the thread must absolutely be joined. As this object was created with a 'placement new', it has to be explicitely destroyed.

Maybe the program does not care about the result so did not wait for it.

Maybe the object was not constructed because an exception was thrown.

For performance , we may avoid this if the return type is a POD (Plain Old Data), which has no constructors.

template<class Ftor, class Thr>
rpa::future< Ftor, Thr >::future ( Ftor &  aFtor,
Thr *  ptrThr 
) [inline]

The constructor launches the sub-thread.

We MUST cross a memory barrier before launching the sub-threads : Because of '_ptrFtor' and '_ptrThr'. Throws an exception if the thread could not be started.


Member Function Documentation

template<class Ftor, class Thr>
static void* rpa::future< Ftor, Thr >::func ( void *  aThis  )  [inline, static, private]

Called by a sub-thread. Executes the functor and stores its return value in our internal buffer. This returns NULL because nothing else is needed. It may be used in the future to trap exceptions.

Default ctor is unneeded because the result is stored in a raw memory chunk, which is initialised with a placement new.

template<class Ftor, class Thr>
Ftor& rpa::future< Ftor, Thr >::functor_ref ( void   )  [inline]

For reduction because the inner functor may have intermediate results.

template<class Ftor, class Thr>
future rpa::future< Ftor, Thr >::operator= ( const future< Ftor, Thr > &   )  [private]

template<class Ftor, class Thr>
void rpa::future< Ftor, Thr >::sync_end ( void   )  [inline, private]

template<class Ftor, class Thr>
result_type& rpa::future< Ftor, Thr >::wait_end ( void   )  [inline]

This is used for reducing and the internal value may be changed.

template<class Ftor, class Thr>
const result_type& rpa::future< Ftor, Thr >::wait_end_const ( void   )  [inline]

This sub-thread we are waiting for must have been created by the main thread, in which we are actually running. It makes no harm to call it several times, only the first call will do a real join of the thread.


Member Data Documentation

template<class Ftor, class Thr>
Ftor* rpa::future< Ftor, Thr >::_ptrFtor [private]

Not copy the functor to execute, nor the thread that will execute it.

Do not change this value because it is later used by the calling thread.

template<class Ftor, class Thr>
Thr* rpa::future< Ftor, Thr >::_ptrThr [private]

This value is used as a flag too.

template<class Ftor, class Thr>
char rpa::future< Ftor, Thr >::_retRawBuf[sizeof(result_type)] [private]

Raw memory to store a return object, no need of default constructor.

Put at first, to avoid alignments problems. Beware !!! When all 'result' functions called by all the functors of the library, will be able to return 'const result_type &', this temporary storage will not be necessary anymore.


The documentation for this class was generated from the following file:
Generated on Tue Sep 25 10:20:38 2007 for rpa by  doxygen 1.4.7