nlib
nn::nlib::threading::Thread Class Referencefinal

Class to create and start threads. More...

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

Public Types

typedef void(* ThreadFunc) (void *arg)
 typedef for the function to run in the thread.
 

Public Member Functions

errno_t Join () noexcept
 Waits for a thread to complete execution. More...
 
errno_t Detach () noexcept
 Detaches the thread. More...
 
bool IsJoinable () const noexcept
 Checks whether a join is possible. More...
 
errno_t GetPriority (int32_t *priority) noexcept
 Gets the thread priority. More...
 
errno_t ChangePriority (int32_t priority) noexcept
 Sets the thread priority. More...
 
errno_t SetAffinity (uint32_t affinity) noexcept
 Sets the processor affinity mask for the thread. More...
 
nlib_thread GetNativeHandle () const noexcept
 Gets the implementation dependent thread ID. More...
 
Basic Member Functions
constexpr Thread () noexcept
 Instantiates the object with default parameters (default constructor).
 
 ~Thread () noexcept
 Destructor. Joins if the thread has not been joined.
 
Threadassign (Thread &rhs, move_tag)
 Assigns the object by using swap for a move.
 
 Thread (Thread &rhs, move_tag)
 Instantiates the object by using swap for a move.
 
 Thread (Thread &&rhs)
 Instantiates the object (move constructor). This function is useful when using C++11.
 
Threadoperator= (Thread &&rhs)
 Move assignment operator. This function is useful when using C++11.
 
void swap (Thread &rhs) noexcept
 Swaps the contents of an object.
 
bool operator== (const Thread &rhs) noexcept
 Compares threads using the nlib_thread_equal function.
 
bool operator!= (const Thread &rhs) noexcept
 Compares threads using the nlib_thread_equal function.
 
Member that directly passes a pointer to start a thread.
errno_t StartRaw (ThreadFunc func, void *arg) noexcept
 Starts running a function that has one void* type argument in a different thread. More...
 
errno_t StartRaw (ThreadFunc func, void *arg, const ThreadSettings &settings) noexcept
 Starts running a function that has one void* type argument in a different thread. More...
 
errno_t StartRaw (const ThreadSettings &settings, ThreadArg<>::Func func) noexcept
 Starts a function that has no arguments in a different thread. More...
 
errno_t StartRaw (ThreadArg<>::Func func) noexcept
 Starts a function that has no arguments in a different thread. More...
 
template<class ThArg >
errno_t StartRaw (const ThreadSettings &settings, UniquePtr< ThArg > &ptr) noexcept
 Starts running another thread. More...
 
template<class ThArg >
errno_t StartRaw (UniquePtr< ThArg > &ptr) noexcept
 Starts running another thread. More...
 
Member that starts a function without arguments, or a function object.
template<class FUNC >
errno_t Start (const ThreadSettings &settings, const FUNC &f, move_tag)
 This function is nearly identical to the version without start options, except that it can specify thread start options.
 
template<class FUNC >
errno_t Start (const FUNC &f, move_tag)
 Starts a function that has no arguments in a different thread. This function is nearly identical to the version with one argument, except for the lack of arguments.
 
template<class FUNC >
errno_t Start (const ThreadSettings &settings, const FUNC &f)
 This function is nearly identical to the version without start options, except that it can specify thread start options.
 
template<class FUNC >
errno_t Start (const FUNC &f)
 Starts a function that has no arguments in a different thread. More...
 
Member that starts functions or function objects that take one argument.
template<class FUNC , class T1 >
errno_t Start (const ThreadSettings &settings, const FUNC &f, const T1 &arg1, move_tag)
 This function is nearly identical to the version without start options, except that it can specify thread start options.
 
template<class FUNC , class T1 >
errno_t Start (const FUNC &f, const T1 &arg1, move_tag)
 Starts a function that has one argument in a different thread. More...
 
template<class FUNC , class T1 >
errno_t Start (const ThreadSettings &settings, const FUNC &f, const T1 &arg1)
 This function is nearly identical to the version without start options, except that it can specify thread start options.
 
template<class FUNC , class T1 >
errno_t Start (const FUNC &f, const T1 &arg1)
 Starts a function that has one argument in a different thread. More...
 
Member that starts functions or function objects that take two arguments.
template<class FUNC , class T1 , class T2 >
errno_t Start (const ThreadSettings &settings, const FUNC &f, const T1 &arg1, const T2 &arg2, move_tag)
 This function is nearly identical to the version without start options, except that it can specify thread start options.
 
template<class FUNC , class T1 , class T2 >
errno_t Start (const FUNC &f, const T1 &arg1, const T2 &arg2, move_tag)
 Starts a function that has two arguments in a different thread. This function is nearly identical to the version with one argument, except that it takes two arguments.
 
template<class FUNC , class T1 , class T2 >
errno_t Start (const ThreadSettings &settings, const FUNC &f, const T1 &arg1, const T2 &arg2)
 This function is nearly identical to the version without start options, except that it can specify thread start options.
 
template<class FUNC , class T1 , class T2 >
errno_t Start (const FUNC &f, const T1 &arg1, const T2 &arg2)
 Starts a function that has two arguments in a different thread. This function is nearly identical to the version with one argument, except that it takes two arguments.
 
Member that starts functions or function objects that take three arguments.
template<class FUNC , class T1 , class T2 , class T3 >
errno_t Start (const ThreadSettings &settings, const FUNC &f, const T1 &arg1, const T2 &arg2, const T3 &arg3, move_tag)
 This function is nearly identical to the version without start options, except that it can specify thread start options.
 
template<class FUNC , class T1 , class T2 , class T3 >
errno_t Start (const FUNC &f, const T1 &arg1, const T2 &arg2, const T3 &arg3, move_tag)
 Starts a function that has three arguments in a different thread. This function is nearly identical to the version with one argument, except that it takes three arguments.
 
template<class FUNC , class T1 , class T2 , class T3 >
errno_t Start (const ThreadSettings &settings, const FUNC &f, const T1 &arg1, const T2 &arg2, const T3 &arg3)
 This function is nearly identical to the version without start options, except that it can specify thread start options.
 
template<class FUNC , class T1 , class T2 , class T3 >
errno_t Start (const FUNC &f, const T1 &arg1, const T2 &arg2, const T3 &arg3)
 Starts a function that has three arguments in a different thread. This function is nearly identical to the version with one argument, except that it takes three arguments.
 
Member that starts functions or function objects that take four arguments.
template<class FUNC , class T1 , class T2 , class T3 , class T4 >
errno_t Start (const ThreadSettings &settings, const FUNC &f, const T1 &arg1, const T2 &arg2, const T3 &arg3, const T4 &arg4, move_tag)
 This function is nearly identical to the version without start options, except that it can specify thread start options.
 
template<class FUNC , class T1 , class T2 , class T3 , class T4 >
errno_t Start (const FUNC &f, const T1 &arg1, const T2 &arg2, const T3 &arg3, const T4 &arg4, move_tag)
 Starts a function that has four arguments in a different thread. This function is nearly identical to the version with one argument, except that it takes four arguments.
 
template<class FUNC , class T1 , class T2 , class T3 , class T4 >
errno_t Start (const ThreadSettings &settings, const FUNC &f, const T1 &arg1, const T2 &arg2, const T3 &arg3, const T4 &arg4)
 This function is nearly identical to the version without start options, except that it can specify thread start options.
 
template<class FUNC , class T1 , class T2 , class T3 , class T4 >
errno_t Start (const FUNC &f, const T1 &arg1, const T2 &arg2, const T3 &arg3, const T4 &arg4)
 Starts a function that has four arguments in a different thread. This function is nearly identical to the version with one argument, except that it takes four arguments.
 
Member that starts functions or function objects that take five arguments.
template<class FUNC , class T1 , class T2 , class T3 , class T4 , class T5 >
errno_t Start (const ThreadSettings &settings, const FUNC &f, const T1 &arg1, const T2 &arg2, const T3 &arg3, const T4 &arg4, const T5 &arg5, move_tag)
 This function is nearly identical to the version without start options, except that it can specify thread start options.
 
template<class FUNC , class T1 , class T2 , class T3 , class T4 , class T5 >
errno_t Start (const FUNC &f, const T1 &arg1, const T2 &arg2, const T3 &arg3, const T4 &arg4, const T5 &arg5, move_tag)
 Starts a function that has five arguments in a different thread. This function is nearly identical to the version with one argument, except that it takes five arguments.
 
template<class FUNC , class T1 , class T2 , class T3 , class T4 , class T5 >
errno_t Start (const ThreadSettings &settings, const FUNC &f, const T1 &arg1, const T2 &arg2, const T3 &arg3, const T4 &arg4, const T5 &arg5)
 This function is nearly identical to the version without start options, except that it can specify thread start options.
 
template<class FUNC , class T1 , class T2 , class T3 , class T4 , class T5 >
errno_t Start (const FUNC &f, const T1 &arg1, const T2 &arg2, const T3 &arg3, const T4 &arg4, const T5 &arg5)
 Starts a function that has five arguments in a different thread. This function is nearly identical to the version with one argument, except that it takes five arguments.
 

Static Public Member Functions

static errno_t YieldThread () noexcept
 Calls the nlib_yield function.
 
static errno_t Sleep (const TimeSpan &span) noexcept
 Sleeps only for the duration of span.
 

Detailed Description

Class to create and start threads.

Definition at line 199 of file Thread.h.

Member Function Documentation

nn::nlib::threading::Thread::ChangePriority ( int32_t  priority)
inlinenoexcept

Sets the thread priority.

Parameters
[in]priorityPriority of the thread.
Return values
0Success.
EINVALIndicates that priority is less than 0 or greather than 31.
ESRCHIndicates that any other error was returned.
Description
Makes an internal call to the nlib_thread_setpriority function.

Definition at line 232 of file Thread.h.

nn::nlib::threading::Thread::Detach ( )
inlinenoexcept

Detaches the thread.

Returns
Returns 0 on success.
Description
Makes an internal call to the nlib_thread_detach function.

Definition at line 223 of file Thread.h.

nn::nlib::threading::Thread::GetNativeHandle ( ) const
inlinenoexcept

Gets the implementation dependent thread ID.

Returns
Returns the implementation-dependent thread ID.
Description
Returns a nlib_thread type value.

Definition at line 238 of file Thread.h.

nn::nlib::threading::Thread::GetPriority ( int32_t *  priority)
inlinenoexcept

Gets the thread priority.

Parameters
[out]priorityStores the thread priority.
Return values
0Indicates that the thread priority was obtained.
EINVALIndicates that priority is NULL.
ESRCHIndicates that any other error was returned.
Description
Makes an internal call to the nlib_thread_getpriority function.

Definition at line 229 of file Thread.h.

nn::nlib::threading::Thread::IsJoinable ( ) const
inlinenoexcept

Checks whether a join is possible.

Returns
Returns true if a join is possible.
Implementation in Cafe
The correct result is not returned if native code from GetNativeHandle is used to join or detach.

Definition at line 228 of file Thread.h.

nn::nlib::threading::Thread::Join ( )
inlinenoexcept

Waits for a thread to complete execution.

Returns
Returns 0 on success.
Description
Makes an internal call to the nlib_thread_join function.

Definition at line 213 of file Thread.h.

nn::nlib::threading::Thread::SetAffinity ( uint32_t  affinity)
inlinenoexcept

Sets the processor affinity mask for the thread.

Parameters
[in]affinityThread affinity mask.
Return values
0Success.
Description
Makes an internal call to the nlib_thread_setaffinity function.

Definition at line 235 of file Thread.h.

template<class FUNC , class T1 >
nn::nlib::threading::Thread::Start ( const FUNC &  f,
const T1 &  arg1,
move_tag   
)
inline

Starts a function that has one argument in a different thread.

Parameters
[in]fFunction to run in a different thread.
[in]arg1Argument passed to function f.
Template Parameters
FUNCFunction or function object that takes one argument.
T1Type of argument specified to function f.
Return values
<tt>0</tt>Indicates that the thread has started.
EINVALIndicates that func is NULL.
ENOMEMIndicates that there are not enough system resources.
Description
Adding a move_tag type object to the last argument moves f, arg1. This is implemented using the member swap function.
arg1 and f are const_cast, and set to the data structure created internally by the swap function. The arg1 copy constructor is only started when running f within the thread. (It will be moved in C++11.)

Definition at line 543 of file Thread.h.

template<class FUNC >
nn::nlib::threading::Thread::Start ( const FUNC &  f)
inline

Starts a function that has no arguments in a different thread.

Parameters
[in]fFunction to run in a different thread.
Template Parameters
FUNCFunction or function object that takes one argument.
Return values
<tt>0</tt>Indicates that the thread has started.
EINVALIndicates that func is NULL.
ENOMEMIndicates that there are not enough system resources.

Definition at line 611 of file Thread.h.

template<class FUNC , class T1 >
nn::nlib::threading::Thread::Start ( const FUNC &  f,
const T1 &  arg1 
)
inline

Starts a function that has one argument in a different thread.

Parameters
[in]fFunction to run in a different thread.
[in]arg1Argument passed to function f.
Template Parameters
FUNCFunction or function object that takes one argument.
T1Type of argument specified to function f.
Return values
<tt>0</tt>Indicates that the thread has started.
EINVALIndicates that func is NULL.
ENOMEMIndicates that there are not enough system resources.
Description
Note that arg1 is passed by value. The arg1 copy constructor is called twice at the following times.
  • When it is set to a data structure created internally to be accessed by the execution thread.
  • When passed as an argument of f within the thread. (Will be moved in C++11.)

Definition at line 622 of file Thread.h.

nn::nlib::threading::Thread::StartRaw ( ThreadFunc  func,
void *  arg 
)
inlinenoexcept

Starts running a function that has one void* type argument in a different thread.

Parameters
[in]funcFunction to run.
[in,out]argArguments to func.
Return values
<tt>0</tt>Indicates that the thread has started.
EINVALIndicates that func is NULL.
ENOMEMIndicates that there are not enough system resources.
Description
The contents of arg are not copied because the type is void*. The thread may be started very quickly, but consider the lifespan and the owner of the memory space when passing a pointer to an object.

Definition at line 207 of file Thread.h.

nn::nlib::threading::Thread::StartRaw ( ThreadFunc  func,
void *  arg,
const ThreadSettings settings 
)
noexcept

Starts running a function that has one void* type argument in a different thread.

Parameters
[in]funcFunction to run.
[in,out]argArguments to func.
[in]settingsOption settings for starting a thread.
Return values
<tt>0</tt>Indicates that the thread has started.
EINVALIndicates that func is NULL.
ENOMEMIndicates that there are not enough system resources.
nn::nlib::threading::Thread::StartRaw ( const ThreadSettings settings,
ThreadArg<>::Func  func 
)
inlinenoexcept

Starts a function that has no arguments in a different thread.

Parameters
[in]settingsOption settings for starting a thread.
[in]funcFunction to run.
Return values
<tt>0</tt>Indicates that the thread has started.
EINVALIndicates that func is NULL.
ENOMEMIndicates that there are not enough system resources.

Definition at line 256 of file Thread.h.

nn::nlib::threading::Thread::StartRaw ( ThreadArg<>::Func  func)
inlinenoexcept

Starts a function that has no arguments in a different thread.

Parameters
[in]funcFunction to run.
Return values
<tt>0</tt>Indicates that the thread has started.
EINVALIndicates that func is NULL.
ENOMEMIndicates that there are not enough system resources.

Definition at line 260 of file Thread.h.

template<class ThArg >
nn::nlib::threading::Thread::StartRaw ( const ThreadSettings settings,
UniquePtr< ThArg > &  ptr 
)
inlinenoexcept

Starts running another thread.

Template Parameters
Typefor the ThArg ThreadArg class template.
Parameters
[in]ptrPointer to the object passed to the thread.
[in]settingsOption settings for starting a thread.
Return values
<tt>0</tt>Indicates that the thread has started.
EINVALIndicates that ptr is NULL.
ENOMEMIndicates that there are not enough system resources.

Definition at line 265 of file Thread.h.

template<class ThArg >
nn::nlib::threading::Thread::StartRaw ( UniquePtr< ThArg > &  ptr)
inlinenoexcept

Starts running another thread.

Template Parameters
Anyof the following: ThArg, ThreadArg1, ThreadArg2, ThreadArg3, ThreadArg4, or ThreadArg5.
Parameters
[in]ptrPointer to the object passed to the thread.
Return values
<tt>0</tt>Indicates that the thread has started.
EINVALIndicates that ptr is NULL.
ENOMEMIndicates that there are not enough system resources.
Description
A function like the following can be created to execute this.
void threadFunc1(UniquePtr<ThreadArg<T1> >& ptr) {
// access by ptr->arg1.
}
....
Thread th;
ThreadArg<T1>::ArgType ptr(new ThreadArg<T1>(threadFunc1, ...));
errno_t e = th.StartRaw(ptr);
// The thread starts when e == 0, and the ownership of the memory space for ThreadArg<T1> moves to the thread that was started.
The code for the setup of the data structure and the function is not clean. The ownership of the memory space of the ThreadArg<T1> data structure that is being passed as an argument has been moved before use. The cost of calling the thread is reduced when the copy constructor execution cost cannot be ignored.

Definition at line 274 of file Thread.h.


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