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

Class that calls the result of thread execution and outputs it in a thread safe manner. This class is similar to the std::promise class of C++11. More...

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

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

Public Types

typedef Future< R > FutureType
 The type for Future<R>.
 

Public Member Functions

errno_t SetValue (const R &value)
 Sets a value, and makes it possible to use Future to extract it. More...
 
errno_t SetError (errno_t e) noexcept
 Sets an error value. More...
 
errno_t GetFuture (Future< R > *p) noexcept
 Sets Future to get the result of execution. More...
 
void swap (Promise &rhs) noexcept
 Swaps an object. More...
 
Basic Member Functions
 Promise () noexcept
 Instantiates the object with default parameters (default constructor).
 
Promiseassign (Promise &rhs, move_tag)
 Assigns the object by using swap for a move.
 
 Promise (Promise &rhs, move_tag)
 Instantiates the object by using swap for a move.
 
 Promise (Promise &&rhs)
 Instantiates the object (move constructor). This function is useful when using C++11.
 
Promiseoperator= (Promise &&rhs)
 Move assignment operator. This function is useful when using C++11.
 
errno_t Init () noexcept
 Initializes the object. More...
 

Detailed Description

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

Class that calls the result of thread execution and outputs it in a thread safe manner. This class is similar to the std::promise class of C++11.

Template Parameters
RThe type of the value to output. You can also specify void.
Description
It is easier to use the PackagedTask class instead of using this class directly.
When you do use this class, use it as follows. Note that a copy constructor and assignment operator are not defined.
void threadFunc(ThreadArg<Promise<int> >::ArgType& ptr) {
ptr->arg1.SetValue(1);
}
....
ThreadArg<Promise<int> >::ArgType p(new ThreadArg<Promise<int> >());
Future<int> myfuture;
p->arg1.GetFuture(&myfuture);
p->func = threadFunc;
Thread th;
th.Start(p);
th.Join(); // The timing for Join is freely defined. It may also be detached.
int result = myfuture.Get(); // Possible to end without getting a result with a timeout or similar method.

Definition at line 41 of file Future.h.

Member Function Documentation

◆ GetFuture()

template<class R>
nn::nlib::threading::Promise< R >::GetFuture ( Future< R > *  p)
inlinenoexcept

Sets Future to get the result of execution.

Parameters
[in]pPointer to Future.
Return values
0Success.
EINVALIndicates that p is NULL.
ENOMEMIndicates that memory allocation failed to store the value.

Definition at line 323 of file Future.h.

◆ Init()

template<class R>
nn::nlib::threading::Promise< R >::Init ( )
inlinenoexcept

Initializes the object.

Return values
0Success.
ENOMEMmemory allocation failed.
EALREADYAlready initialized.

Definition at line 321 of file Future.h.

◆ SetError()

template<class R>
nn::nlib::threading::Promise< R >::SetError ( errno_t  e)
inlinenoexcept

Sets an error value.

Parameters
[in]eAn error value.
Return values
0Error value successfully set.
EALREADYError value already set.
ENOMEMIndicates that either memory allocation failed or that the Init function was not called ahead of time.
Description
If a nonzero error value has been set, the Future::Get(R* ptr) function can return an error value as its return value.

Definition at line 322 of file Future.h.

◆ SetValue()

template<class R>
nn::nlib::threading::Promise< R >::SetValue ( const R &  value)

Sets a value, and makes it possible to use Future to extract it.

Parameters
[in]valueThe value to set.
Return values
0Success.
ENOMEMIndicates that memory allocation failed to store the value.
EALREADYIndicates that a value has already been set.
The value is set in a thread-safe manner for Future.

Definition at line 342 of file Future.h.

◆ swap()

template<class R>
nn::nlib::threading::Promise< R >::swap ( Promise< R > &  rhs)
inlinenoexcept

Swaps an object.

Parameters
[in]rhsObject to swap.

Definition at line 326 of file Future.h.


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