nlib
nn::nlib::threading::Future< R > Class Template Reference

Class that gets the output of a different thread executing in a thread safe manner. This class is similar to the std::shared_future class of C++11. More...

#include "nn/nlib/threading/Future.h"

Inherits nn::nlib::threading::detail::FutureBase< Derived >.

Public Member Functions

Get ()
 Calls Get(R* ptr) after creating an object for the type that will be used internally as the value. More...
 
errno_t Get (R *ptr)
 Waits until a result is acquired, and then gets the result. More...
 
bool IsValid () const noexcept
 
bool IsReady () const noexcept
 Returns whether a value has been set for Future (whether Promise set a value or an error). More...
 
errno_t Wait () noexcept
 Waits for conditions that allow results to be acquired.
 
NLIB_CHECK_RESULT errno_t WaitFor (const TimeSpan &span) noexcept
 Waits for conditions that allow results to be acquired, for a specified amount of time. More...
 
NLIB_CHECK_RESULT errno_t WaitUntil (const DateTime &datetime) noexcept
 Waits for conditions that allow results to be acquired, until the defined time. More...
 
template<class RNEXT >
errno_t Then (Future< RNEXT > *next, RNEXT(*cont)(Future< R > &)) noexcept
 Registers the process to continue (continuation) to this future. More...
 
errno_t MakeSharedFrom (const Future &f) noexcept
 Shares a Future so that it may be viewed. More...
 
Basic Member Functions
 Future () noexcept
 Instantiates the object with default parameters (default constructor).
 
Futureassign (Future &rhs, move_tag)
 Assigns the object by using swap for a move.
 
 Future (Future &rhs, move_tag)
 Instantiates the object by using swap for a move.
 
 Future (Future &&rhs)
 Instantiates the object (move constructor). This function is useful when using C++11.
 
Futureoperator= (Future &&rhs)
 Move assignment operator. This function is useful when using C++11.
 

Detailed Description

template<class R>
class nn::nlib::threading::Future< R >

Class that gets the output of a different thread executing in a thread safe manner. This class is similar to the std::shared_future class of C++11.

Template Parameters
RThe type of the value to get. You can also specify void.
Description
Future can be associated with Promise by using Promise<R>::GetFuture or PackagedTask<T>::GetFuture. Future contains space shared with the function's execution thread. Future provides a framework to safely get the result set by the function's execution thread.
This class is implemented using internal conditional variables and shared pointers. A brief code sample is provided in the description of PackagedTask.

Definition at line 13 of file AsyncFileIo.h.

Member Function Documentation

§ Get() [1/2]

template<class R>
nn::nlib::threading::Future< R >::Get ( )
inline

Calls Get(R* ptr) after creating an object for the type that will be used internally as the value.

Returns
The result of Future.
Description
Note that the R constructor may throw an exception when using this function.
In addition, you cannot get the error that might occur when you act to get the value.

Definition at line 514 of file Future.h.

§ Get() [2/2]

template<class R>
nn::nlib::threading::Future< R >::Get ( R *  ptr)
inline

Waits until a result is acquired, and then gets the result.

Parameters
[in,out]ptrPointer where a value is set.
Returns
0 Returns whether a value has been set in ptr. All other values are an error value set by the thread.
Description
A Get function that does not take arguments may be used when Future<void>.

Definition at line 521 of file Future.h.

§ IsReady()

template<class R>
nn::nlib::threading::Future< R >::IsReady ( ) const
inlinenoexcept

Returns whether a value has been set for Future (whether Promise set a value or an error).

Returns
Returns true if a value has been set to Future.

Definition at line 540 of file Future.h.

§ IsValid()

template<class R>
nn::nlib::threading::Future< R >::IsValid ( ) const
inlinenoexcept

/brief Returns whether Future is associated with Promise.

Returns
Returns true if Future is associated with Promise.

Definition at line 539 of file Future.h.

§ MakeSharedFrom()

template<class R>
nn::nlib::threading::Future< R >::MakeSharedFrom ( const Future< R > &  f)
inlinenoexcept

Shares a Future so that it may be viewed.

Parameters
[in]fFuture object to share.
Returns
Returns 0 on success.
Description
Future includes a view counter. You may use this function to wait for the same result from multiple threads.
To prevent the proliferation of shared pointers, the Future class does not define a copy or assignment operator.

Definition at line 552 of file Future.h.

§ Then()

template<class R>
template<class RNEXT >
nn::nlib::threading::Future< R >::Then ( Future< RNEXT > *  next,
RNEXT(*)(Future< R > &)  cont 
)
inlinenoexcept

Registers the process to continue (continuation) to this future.

Template Parameters
RNEXTReturn value type of the continuation.
Parameters
[out]nextType of Future that corresponds to the continuation.
[in]contFunction registered as a continuation.
Return values
0Success.
EINVALFuture is not associated with promise.
ENOMEMNot enough memory.
Anothererror may be returned from a lower layer.
Description
In asynchronous programming, sometimes a new process is started and has data passed to it when an asynchronous process completes. You can wait for Future by using Then to relate the continuing process to the asynchronous process.
The use of Then makes it easier to write a process where the main thread receives the in-progress information of the subthread at appropriate points.

Definition at line 549 of file Future.h.

§ WaitFor()

template<class R>
nn::nlib::threading::Future< R >::WaitFor ( const TimeSpan span)
inlinenoexcept

Waits for conditions that allow results to be acquired, for a specified amount of time.

Parameters
[in]spanDuration to wait for.
Return values
0Indicates that a result can be acquired.
ETIMEDOUTIndicates that a timeout has occurred.
Description
// Waits 10 msec for results from a different thread.
if (future.WaitFor(TimeSpan(0, 0, 10)) == 0) {
result = future.Get();
}

Definition at line 542 of file Future.h.

§ WaitUntil()

template<class R>
nn::nlib::threading::Future< R >::WaitUntil ( const DateTime datetime)
inlinenoexcept

Waits for conditions that allow results to be acquired, until the defined time.

Parameters
[in]datetimeWait limit time.
Return values
0Indicates that a result can be acquired.
ETIMEDOUTIndicates that a timeout has occurred.

Definition at line 545 of file Future.h.


The documentation for this class was generated from the following files: