Embedded Multicore Building Blocks V1.0.0
Classes | Public Member Functions | Static Public Member Functions | List of all members
embb::base::Thread Class Reference

Represents a thread of execution. More...

#include <thread.h>

Classes

class  ID
 Unique ID of a thread that can be compared with other IDs. More...
 

Public Member Functions

template<typename Function >
 Thread (Function function)
 Creates and runs a thread with zero-argument start function. More...
 
template<typename Function >
 Thread (CoreSet &core_set, Function function)
 Creates and runs a thread with zero-argument start function. More...
 
template<typename Function >
 Thread (CoreSet &core_set, embb_thread_priority_t priority, Function function)
 Creates and runs a thread with zero-argument start function. More...
 
template<typename Function , typename Arg >
 Thread (Function function, Arg arg)
 Creates and runs a thread with one-argument thread start function. More...
 
template<typename Function , typename Arg1 , typename Arg2 >
 Thread (Function function, Arg1 arg1, Arg2 arg2)
 Creates and runs a thread with two-argument thread start function. More...
 
void Join ()
 Waits until the thread has finished execution. More...
 
ID GetID ()
 Returns the thread ID. More...
 

Static Public Member Functions

static unsigned int GetThreadsMaxCount ()
 Returns the maximum number of threads handled by EMB2. More...
 
static void SetThreadsMaxCount (unsigned int max_count)
 Sets the maximum number of threads handled by EMB2. More...
 
static ID CurrentGetID ()
 Returns the ID of the current thread. More...
 
static void CurrentYield ()
 Reschedule the current thread for later execution. More...
 

Detailed Description

Represents a thread of execution.

Provides an abstraction from platform-specific threading implementations to create, manage, and join threads of execution. Support for thread-to-core affinities is given on thread creation by using the core set functionality.

This class is essentially a wrapper for the underlying C implementation.

Constructor & Destructor Documentation

template<typename Function >
embb::base::Thread::Thread ( Function  function)
explicit

Creates and runs a thread with zero-argument start function.

Note
If the function is passed as a temporary object when creating a thread, this might be interpreted as a function declaration ("most vexing parse"). C++11 resolves this by using curly braces for initialization.
Exceptions
NoMemoryExceptionif not enough memory is available
ErrorExceptionin case of another error
Dynamic memory allocation
A small constant amount of memory to store the function. This memory is freed the thread is joined.
Concurrency
Not thread-safe
Template Parameters
FunctionFunction object type
Parameters
[in]functionCopyable function object, callable without arguments
template<typename Function >
embb::base::Thread::Thread ( CoreSet core_set,
Function  function 
)

Creates and runs a thread with zero-argument start function.

Note
If the function is passed as a temporary object when creating a thread, this might be interpreted as a function declaration ("most vexing parse"). C++11 resolves this by using curly braces for initialization.
Exceptions
NoMemoryExceptionif not enough memory is available
ErrorExceptionin case of another error
Dynamic memory allocation
A small constant amount of memory to store the function. This memory is freed the thread is joined.
Concurrency
Not thread-safe
Template Parameters
FunctionFunction object type
Parameters
[in]core_setSet of cores on which the thread shall be executed.
[in]functionCopyable function object, callable without arguments
template<typename Function >
embb::base::Thread::Thread ( CoreSet core_set,
embb_thread_priority_t  priority,
Function  function 
)

Creates and runs a thread with zero-argument start function.

Note
If the function is passed as a temporary object when creating a thread, this might be interpreted as a function declaration ("most vexing parse"). C++11 resolves this by using curly braces for initialization.
Exceptions
NoMemoryExceptionif not enough memory is available
ErrorExceptionin case of another error
Dynamic memory allocation
A small constant amount of memory to store the function. This memory is freed the thread is joined.
Concurrency
Not thread-safe
Template Parameters
FunctionFunction object type
Parameters
[in]core_setSet of cores on which the thread shall be executed.
[in]priorityPriority of the new thread.
[in]functionCopyable function object, callable without arguments
template<typename Function , typename Arg >
embb::base::Thread::Thread ( Function  function,
Arg  arg 
)

Creates and runs a thread with one-argument thread start function.

Note
If the function is passed as a temporary object when creating a thread, this might be interpreted as a function declaration ("most vexing parse"). C++11 resolves this by using curly braces for initialization.
Exceptions
NoMemoryExceptionif not enough memory is available
ErrorExceptionin case of another error
Dynamic memory allocation
A small constant amount of memory to store the function. This memory is freed the thread is joined.
Concurrency
Not thread-safe
Template Parameters
FunctionFunction object type
ArgumentType of argument
Parameters
[in]functionCopyable function object, callable with one argument
[in]argArgument for function (must be copyable)
template<typename Function , typename Arg1 , typename Arg2 >
embb::base::Thread::Thread ( Function  function,
Arg1  arg1,
Arg2  arg2 
)

Creates and runs a thread with two-argument thread start function.

Note
If the function is passed as a temporary object when creating a thread, this might be interpreted as a function declaration ("most vexing parse"). C++11 resolves this by using curly braces for initialization.
Exceptions
NoMemoryExceptionif not enough memory is available
ErrorExceptionin case of another error
Dynamic memory allocation
A small constant amount of memory to store the function. This memory is freed the thread is joined.
Concurrency
Not thread-safe
Template Parameters
FunctionFunction object type
Arg1Type of first argument
Arg2Type of second argument
Parameters
[in]functionCopyable function object, callable with two arguments
[in]arg1First argument for function (must be copyable)
[in]arg2Second argument for function (must be copyable)

Member Function Documentation

static unsigned int embb::base::Thread::GetThreadsMaxCount ( )
static

Returns the maximum number of threads handled by EMB2.

See embb_thread_get_max_count() for a description of the semantics.

Returns
Maximum number of threads
Concurrency
Thread-safe and lock-free
See also
SetThreadsMaxCount()
static void embb::base::Thread::SetThreadsMaxCount ( unsigned int  max_count)
static

Sets the maximum number of threads handled by EMB2.

Concurrency
Not thread-safe
See also
GetThreadsMaxCount()
Parameters
[in]max_countMaximum number of threads
static ID embb::base::Thread::CurrentGetID ( )
static

Returns the ID of the current thread.

The ID is only valid within the calling thread.

Returns
ID of the calling thread
Concurrency
Thread-safe
static void embb::base::Thread::CurrentYield ( )
static

Reschedule the current thread for later execution.

This is only a request, the realization depends on the implementation and the scheduler employed by the operating system.

Concurrency
Thread-safe
void embb::base::Thread::Join ( )

Waits until the thread has finished execution.

Precondition
The thread has been created but not yet been joined.
Postcondition
The thread has finished execution and dynamic memory allocated during creation has been freed.
Concurrency
Not thread-safe
ID embb::base::Thread::GetID ( )

Returns the thread ID.

Returns
ID of the thread
Concurrency
Thread-safe