nlib
nn::nlib::threading Namespace Reference

Unicode related functionality is implemented. More...

Namespaces

 this_thread
 Implements functions related to current thread behavior.
 

Classes

struct  AdoptLockType
 Used in ScopedLock and UniqueLock by tag type. More...
 
class  AsyncFileIo
 Class that wraps asynchronous file I/O. More...
 
class  AsyncFileIoService
 Class that controls asynchronous file I/O. More...
 
class  Barrier
 Synchronizes an arbitrary number of threads. More...
 
class  CondVar
 Conditional variable for synchronization. More...
 
class  CondVarForSharedCriticalSection
 A conditional variable for SharedCriticalSection and nlib_rwlock. This variable can be used in the same manner as CondVar. More...
 
class  CriticalSection
 Reentrant mutex. More...
 
struct  DeferLockType
 Used in ScopedLock and UniqueLock by tag type. More...
 
class  Future
 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...
 
class  LockFreeThreadPool
 This thread pool is suitable for deploying fine grain tasks. More...
 
class  PackagedTask
 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...
 
class  Promise
 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...
 
class  ScopedLock
 Wraps objects like CriticalSection. Locks with a constructor, and unlocks with a destructor. More...
 
class  Semaphore
 Implements a semaphore. More...
 
class  SharedCriticalSection
 Implements a read/write lock. Used when multiple threads simultaneously read data, and a single thread writes data. More...
 
class  SimpleCriticalSection
 Simplest critical section. Not reentrant. More...
 
class  Thread
 Class to create and start threads. More...
 
struct  ThreadArg
 Structure to run a thread in a Thread class. More...
 
class  ThreadPool
 Tasks may be deployed to a pool of threads that have been already created. More...
 
struct  ThreadSettings
 Class to wrap nlib_thread_attr. nlib_thread_attr_init() and nlib_thread_attr_destroy() are run automatically as required. More...
 
class  TimedCriticalSection
 Critical section that can timeout in reentrant. More...
 
class  Tls
 Class for handling thread local storage. More...
 
struct  TryToLockType
 Used in ScopedLock and UniqueLock by tag type. More...
 
class  UniqueLock
 Class to wrap objects like CriticalSection. More...
 
class  WhenFutureComplete
 Defines a Future for which to set the value, under the condition that a value has been set for the Future. More...
 

Functions

template<class FutureResult >
errno_t GetFutureError (FutureResult &result) noexcept
 Function that enables systems to get error values when threads hold an error value. More...
 
template<class R >
errno_t MakeReadyFuture (Future< R > *future, const R &value)
 Creates a Future with preset values. More...
 
size_t GetHardwareConcurrency () noexcept
 Returns the number of hardware threads. More...
 
Async (Function With No Arguments)
template<class FUNC , class R >
errno_t Async (Future< R > *future, const FUNC &func, const ThreadSettings &settings)
 Async that allows the user to configure the start of the thread.
 
template<class FUNC , class R >
errno_t Async (Future< R > *future, const FUNC &func, const ThreadSettings &settings, move_tag)
 Async where the user may configure the thread start, and where arguments and the function object are moved.
 
template<class FUNC , class R >
errno_t Async (Future< R > *future, const FUNC &func)
 Runs the function in a different thread, and makes it possible to use Future to extract the result.
 
template<class FUNC , class R >
errno_t Async (Future< R > *future, const FUNC &func, move_tag)
 Async that moves arguments and function objects.
 
Async (Function With One Argument)
template<class FUNC , class R , class T1 >
errno_t Async (Future< R > *future, const FUNC &func, const T1 &arg1, const ThreadSettings &settings)
 Async that allows the user to configure the start of the thread.
 
template<class FUNC , class R , class T1 >
errno_t Async (Future< R > *future, const FUNC &func, const T1 &arg1, const ThreadSettings &settings, move_tag)
 Async where the user may configure the thread start, and where arguments and the function object are moved.
 
template<class FUNC , class R , class T1 >
errno_t Async (Future< R > *future, const FUNC &func, const T1 &arg1)
 Runs the function in a different thread, and makes it possible to use Future to extract the result. More...
 
template<class FUNC , class R , class T1 >
errno_t Async (Future< R > *future, const FUNC &func, const T1 &arg1, move_tag)
 Async that moves arguments and function objects. More...
 
Async (Function With Two Arguments)
template<class FUNC , class R , class T1 , class T2 >
errno_t Async (Future< R > *future, const FUNC &func, const T1 &arg1, const T2 &arg2, const ThreadSettings &settings)
 Async that allows the user to configure the start of the thread.
 
template<class FUNC , class R , class T1 , class T2 >
errno_t Async (Future< R > *future, const FUNC &func, const T1 &arg1, const T2 &arg2, const ThreadSettings &settings, move_tag)
 Async where the user may configure the thread start, and where arguments and the function object are moved.
 
template<class FUNC , class R , class T1 , class T2 >
errno_t Async (Future< R > *future, const FUNC &func, const T1 &arg1, const T2 &arg2)
 Runs the function in a different thread and makes it possible to extract the result according to the future. More...
 
template<class FUNC , class R , class T1 , class T2 >
errno_t Async (Future< R > *future, const FUNC &func, const T1 &arg1, const T2 &arg2, move_tag)
 Async that moves arguments and function objects.
 
Async (Function With Three Arguments)
template<class FUNC , class R , class T1 , class T2 , class T3 >
errno_t Async (Future< R > *future, const FUNC &func, const T1 &arg1, const T2 &arg2, const T3 &arg3, const ThreadSettings &settings)
 Async that allows the user to configure the start of the thread.
 
template<class FUNC , class R , class T1 , class T2 , class T3 >
errno_t Async (Future< R > *future, const FUNC &func, const T1 &arg1, const T2 &arg2, const T3 &arg3, const ThreadSettings &settings, move_tag)
 Async where the user may configure the thread start, and where arguments and the function object are moved.
 
template<class FUNC , class R , class T1 , class T2 , class T3 >
errno_t Async (Future< R > *future, const FUNC &func, const T1 &arg1, const T2 &arg2, const T3 &arg3)
 Runs the function in a different thread and makes it possible to extract the result according to the future. More...
 
template<class FUNC , class R , class T1 , class T2 , class T3 >
errno_t Async (Future< R > *future, const FUNC &func, const T1 &arg1, const T2 &arg2, const T3 &arg3, move_tag)
 Async that moves arguments and function objects.
 
Async (Function With Four Arguments)
template<class FUNC , class R , class T1 , class T2 , class T3 , class T4 >
errno_t Async (Future< R > *future, const FUNC &func, const T1 &arg1, const T2 &arg2, const T3 &arg3, const T4 &arg4, const ThreadSettings &settings)
 Async that allows the user to configure the start of the thread.
 
template<class FUNC , class R , class T1 , class T2 , class T3 , class T4 >
errno_t Async (Future< R > *future, const FUNC &func, const T1 &arg1, const T2 &arg2, const T3 &arg3, const T4 &arg4, const ThreadSettings &settings, move_tag)
 Async where the user may configure the thread start, and where arguments and the function object are moved.
 
template<class FUNC , class R , class T1 , class T2 , class T3 , class T4 >
errno_t Async (Future< R > *future, const FUNC &func, const T1 &arg1, const T2 &arg2, const T3 &arg3, const T4 &arg4)
 Runs the function in a different thread and makes it possible to extract the result according to the future. More...
 
template<class FUNC , class R , class T1 , class T2 , class T3 , class T4 >
errno_t Async (Future< R > *future, const FUNC &func, const T1 &arg1, const T2 &arg2, const T3 &arg3, const T4 &arg4, move_tag)
 Async that moves arguments and function objects.
 
Async (Function With Five Arguments)
template<class FUNC , class R , class T1 , class T2 , class T3 , class T4 , class T5 >
errno_t Async (Future< R > *future, const FUNC &func, const T1 &arg1, const T2 &arg2, const T3 &arg3, const T4 &arg4, const T5 &arg5, const ThreadSettings &settings)
 Async that allows the user to configure the start of the thread.
 
template<class FUNC , class R , class T1 , class T2 , class T3 , class T4 , class T5 >
errno_t Async (Future< R > *future, const FUNC &func, const T1 &arg1, const T2 &arg2, const T3 &arg3, const T4 &arg4, const T5 &arg5, const ThreadSettings &settings, move_tag)
 Async where the user may configure the thread start, and where arguments and the function object are moved.
 
template<class FUNC , class R , class T1 , class T2 , class T3 , class T4 , class T5 >
errno_t Async (Future< R > *future, const FUNC &func, const T1 &arg1, const T2 &arg2, const T3 &arg3, const T4 &arg4, const T5 &arg5)
 Runs the function in a different thread and makes it possible to extract the result according to the future. More...
 
template<class FUNC , class R , class T1 , class T2 , class T3 , class T4 , class T5 >
errno_t Async (Future< R > *future, const FUNC &func, const T1 &arg1, const T2 &arg2, const T3 &arg3, const T4 &arg4, const T5 &arg5, move_tag)
 Async that moves arguments and function objects.
 

Variables

constexpr const AdoptLockType adoptLock = {}
 An AdoptLockType-type value.
 
constexpr const TryToLockType tryToLock = {}
 A TryToLockType-type value.
 
constexpr const DeferLockType deferLock = {}
 A DeferLockType-type value.
 

Detailed Description

Unicode related functionality is implemented.

Description
The following classes and functions are implemented in the nn::nlib::threading namespace.

Function Documentation

◆ Async() [1/6]

template<class FUNC , class R , class T1 >
nn::nlib::threading::Async ( Future< R > *  future,
const FUNC &  func,
const T1 &  arg1 
)
inline

Runs the function in a different thread, and makes it possible to use Future to extract the result.

Template Parameters
RType of the return value of func.
FUNCType of func.
T1Argument passed to func.
Parameters
[in]futurePointer to Future<R>.
[in]funcFunction or function object that takes one argument.
[in]arg1Argument passed to the function.
Return values
0Indicates that the thread has started to run.
EINVALIndicates that future or func is NULL.
ENOMEMIndicates that internal memory allocation failed.
Description
This function is similar to std::async in the C++11 standard library. A version that can configure ThreadSettings with the last argument has also been implemented.
To run functions in different threads and receive the results in Future, use the following code.
int Plus1(int arg1) { return arg1 + 1; }
....
Future<int> myfuture;
errno_t e = Async(&myfuture, Plus1, 1);
if (e != 0) { ERROR }
int result = myfuture.Get(); // 2
Adding nn::nlib::move_tag to the end as an argument moves the argument (implemented by a swap member function) to reduce copies of the object.

Definition at line 1863 of file Future.h.

◆ Async() [2/6]

template<class FUNC , class R , class T1 >
nn::nlib::threading::Async ( Future< R > *  future,
const FUNC &  func,
const T1 &  arg1,
move_tag   
)
inline

Async that moves arguments and function objects.

Template Parameters
RType of the return value of func.
FUNCType of func.
T1Argument passed to func.
Parameters
[in]futurePointer to Future<R>.
[in]funcFunction or function object that takes one argument.
[in]arg1Argument passed to the function.
Return values
0Indicates that the thread has started to run.
EINVALIndicates that future or func is NULL.
ENOMEMIndicates that internal memory allocation failed.
Description
Internally, func and arg1 are cast using const_cast and moved (using swap). This reduces copies of the object.

Definition at line 1870 of file Future.h.

◆ Async() [3/6]

template<class FUNC , class R , class T1 , class T2 >
nn::nlib::threading::Async ( Future< R > *  future,
const FUNC &  func,
const T1 &  arg1,
const T2 &  arg2 
)
inline

Runs the function in a different thread and makes it possible to extract the result according to the future.

Template Parameters
RType of the return value of func.
FUNCType of func.
T1Argument passed to func.
T2Argument passed to func.
Parameters
[in]futurePointer to Future<R>.
[in]funcPointer to the function to run.
[in]arg1Argument passed to the function.
[in]arg2Argument passed to the function.
Return values
0Indicates that the thread has started to run.
EINVALIndicates that future or func is NULL.
ENOMEMIndicates that internal memory allocation failed.

Definition at line 1889 of file Future.h.

◆ Async() [4/6]

template<class FUNC , class R , class T1 , class T2 , class T3 >
nn::nlib::threading::Async ( Future< R > *  future,
const FUNC &  func,
const T1 &  arg1,
const T2 &  arg2,
const T3 &  arg3 
)
inline

Runs the function in a different thread and makes it possible to extract the result according to the future.

Template Parameters
RType of the return value of func.
FUNCType of func.
T1Argument passed to func.
T2Argument passed to func.
T3Argument passed to func.
Parameters
[in]futurePointer to Future<R>.
[in]funcPointer to the function to run.
[in]arg1Argument passed to the function.
[in]arg2Argument passed to the function.
[in]arg3Argument passed to the function.
Return values
0Indicates that the thread has started to run.
EINVALIndicates that future or func is NULL.
ENOMEMIndicates that internal memory allocation failed.

Definition at line 1916 of file Future.h.

◆ Async() [5/6]

template<class FUNC , class R , class T1 , class T2 , class T3 , class T4 >
nn::nlib::threading::Async ( Future< R > *  future,
const FUNC &  func,
const T1 &  arg1,
const T2 &  arg2,
const T3 &  arg3,
const T4 &  arg4 
)
inline

Runs the function in a different thread and makes it possible to extract the result according to the future.

Template Parameters
RType of the return value of func.
FUNCType of func.
T1Argument passed to func.
T2Argument passed to func.
T3Argument passed to func.
T4Argument passed to func.
Parameters
[in]futurePointer to Future<R>.
[in]funcPointer to the function to run.
[in]arg1Argument passed to the function.
[in]arg2Argument passed to the function.
[in]arg3Argument passed to the function.
[in]arg4Argument passed to the function.
Return values
0Indicates that the thread has started to run.
EINVALIndicates that future or func is NULL.
ENOMEMIndicates that internal memory allocation failed.

Definition at line 1944 of file Future.h.

◆ Async() [6/6]

template<class FUNC , class R , class T1 , class T2 , class T3 , class T4 , class T5 >
nn::nlib::threading::Async ( Future< R > *  future,
const FUNC &  func,
const T1 &  arg1,
const T2 &  arg2,
const T3 &  arg3,
const T4 &  arg4,
const T5 &  arg5 
)
inline

Runs the function in a different thread and makes it possible to extract the result according to the future.

Template Parameters
RType of the return value of func.
FUNCType of func.
T1Argument passed to func.
T2Argument passed to func.
T3Argument passed to func.
T4Argument passed to func.
T5Argument passed to func.
Parameters
[in]futurePointer to Future<R>.
[in]funcPointer to the function to run.
[in]arg1Argument passed to the function.
[in]arg2Argument passed to the function.
[in]arg3Argument passed to the function.
[in]arg4Argument passed to the function.
[in]arg5Argument passed to the function.
Return values
0Indicates that the thread has started to run.
EINVALIndicates that future or func is NULL.
ENOMEMIndicates that internal memory allocation failed.

Definition at line 1974 of file Future.h.

◆ GetFutureError()

template<class FutureResult >
nn::nlib::threading::GetFutureError ( FutureResult &  result)
inlinenoexcept

Function that enables systems to get error values when threads hold an error value.

Template Parameters
FutureResultType of the value of Future in the return value of the function.
Parameters
[in,out]resultReturn value of the function.
Returns
Returns the error value.
Description
The default is a function that returns 0. Specialize the template to use.
When the thread must be made to return an error, the error value is included in the return value type of the function. Defining this function allows the developer to clearly describe what is an error value against the library code.
Library codes that must get a thread execution error, use GetFutureError to get the error.

Definition at line 35 of file Future.h.

◆ GetHardwareConcurrency()

size_t nn::nlib::threading::GetHardwareConcurrency ( )
inlinenoexcept

Returns the number of hardware threads.

Returns
Returns the number of hardware threads.

Definition at line 814 of file Thread.h.

◆ MakeReadyFuture()

template<class R >
nn::nlib::threading::MakeReadyFuture ( Future< R > *  future,
const R &  value 
)
inline

Creates a Future with preset values.

Template Parameters
RValue type of the future.
Parameters
[in,out]futurePointer to the Future object that sets future.
[in]valueThe value to set.
Return values
0Success.
EINVALIndicates that future is NULL.
Description
In cases where I/F uses Future but the process results are already ready, a value is set to Future without starting the thread.
The following function is used in this case.

Definition at line 1990 of file Future.h.