nlib
nn::nlib::threading::PackagedTask< T > Class Template Reference

Class that wraps a function to run in a different thread, and gets the return value in a thread safe manner. This class is similar to the std::packaged_task class of C++11. More...

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

Public Types

typedef PackagedTask< T > ThisType
 The object type.
 

Public Member Functions

 PackagedTask () noexcept
 Instantiates the object with default parameters (default constructor).
 
template<class FUNC >
errno_t Init (FUNC &func)
 Performs initialization. More...
 
template<class R >
errno_t GetFuture (Future< R > *p) noexcept
 Sets Future to get the result of execution. More...
 
void swap (PackagedTask &rhs) noexcept
 Swap. More...
 

Detailed Description

template<class T>
class nn::nlib::threading::PackagedTask< T >

Class that wraps a function to run in a different thread, and gets the return value in a thread safe manner. This class is similar to the std::packaged_task class of C++11.

Template Parameters
TFunction type. Instantiate by writing in the form of int(int, int). Up to five arguments may be used, and using the void type as a return value is supported.
Description
Supports functions with up to five arguments. Each function may access the data members arg1, arg2, arg3, arg4, arg5.
Instantiate the template as shown below.
int myplus(int x, int y) { return x + y; }
.....
PackagedTask<int(int, int)> task(myplus);
int arg1 = ....;
int arg2 = ....;
Future<int> myfuture;
task.GetFuture(&myfuture);
Thread th;
th.Start(task, arg1, arg2, nn::nlib::move_tag());
th.Join(); //You may join first, join later, or detach.
int result = myfuture.Get(); // Get the result of the function.

Definition at line 931 of file Future.h.

Member Function Documentation

◆ GetFuture()

template<class T>
template<class R >
nn::nlib::threading::PackagedTask< T >::GetFuture ( Future< R > *  p)
noexcept

Sets Future to get the result of execution.

Template Parameters
RType of return value.
Parameters
[in]pPointer to Future.
Return values
0Success.
EINVALIndicates that p is NULL.
ENOMEMIndicates that memory allocation failed to store the value.
Description
Gets Future from the Promise of the object. The result may be acquired from Future immediately after the functions run by PackagedTask are completed. You can use this function if the thread is not joined or detached.

◆ Init()

template<class T>
template<class FUNC >
nn::nlib::threading::PackagedTask< T >::Init ( FUNC &  func)

Performs initialization.

Template Parameters
FUNCType of object that can call a function pointer, and similar.
Parameters
[in]funcObject that may call a function pointer or similar.
Returns
Returns 0 on success.

◆ swap()

template<class T>
nn::nlib::threading::PackagedTask< T >::swap ( PackagedTask< T > &  rhs)
noexcept

Swap.

Parameters
[in]rhsObject to swap.

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