EtcPal
HEAD (unstable)
ETC Platform Abstraction Layer (EtcPal)
|
View other versions:
|
A thread class, modeled after std::thread.
This class is similar to std::thread, with the key advantage that it will work on any threaded platform that EtcPal is ported for, including the embedded RTOS platforms. If your application or library does not need to run on these platforms, consider using std::thread instead.
There are a few differences from std::thread:
This is one of the few EtcPal wrappers that does heap allocation, as the thread function and arguments need to be stored.
Public Member Functions | |
Thread ()=default | |
Create a new thread object which does not yet represent a thread. | |
template<class Function , class... Args, typename std::enable_if<!std::is_arithmetic< Function >::value, bool >::type = true> | |
Thread (Function &&func, Args &&... args) | |
Create a new thread object and associate it with a new thread of execution. More... | |
Thread (unsigned int priority, unsigned int stack_size, const char *name, void *platform_data=nullptr) | |
Create a new thread object which does not yet represent a thread, passing explicit parameters. More... | |
virtual | ~Thread () |
Destroy the thread object. More... | |
Thread (Thread &&other) noexcept | |
Move another thread into this thread. More... | |
Thread & | operator= (Thread &&other) noexcept |
Move another thread into this thread. More... | |
Thread (const Thread &other)=delete | |
Deleted copy constructor - threads are not copyable. | |
Thread & | operator= (const Thread &other)=delete |
Deleted copy assignment operator - threads are not copyable. | |
bool | joinable () const noexcept |
Whether the thread object identifies an active thread of execution. | |
template<class Function , class... Args> | |
Error | Start (Function &&func, Args &&... args) |
Associate this thread object with a new thread of execution. More... | |
Error | Join (int timeout_ms=ETCPAL_WAIT_FOREVER) noexcept |
Wait for the thread to finish execution. More... | |
Error | Terminate () noexcept |
Forcefully kill the thread. More... | |
Getters | |
unsigned int | priority () const noexcept |
Get the priority of this thread (not valid on all platforms). | |
unsigned int | stack_size () const noexcept |
Get the stack size of this thread in bytes (not valid on all platforms). | |
const char * | name () const noexcept |
Get the name of this thread. | |
void * | platform_data () const noexcept |
Get the platform-specific data of this thread. | |
const EtcPalThreadParams & | params () const noexcept |
Get a reference the parameters of this thread. | |
etcpal_thread_os_handle_t | os_handle () const noexcept |
Get the native OS handle of this thread. More... | |
Setters | |
Thread & | SetPriority (unsigned int priority) noexcept |
Set the priority of this thread. More... | |
Thread & | SetStackSize (unsigned int stack_size) noexcept |
Set the stack size of this thread in bytes. More... | |
Thread & | SetName (const char *name) noexcept |
Set the name of this thread. More... | |
Thread & | SetName (const std::string &name) noexcept |
Set the name of this thread. More... | |
Thread & | SetPlatformData (void *platform_data) noexcept |
Set the platform-specific parameter data. More... | |
Thread & | SetParams (const EtcPalThreadParams ¶ms) noexcept |
Set this thread's parameters from an existing EtcPalThreadParams struct. More... | |
Static Public Member Functions | |
static Error | Sleep (unsigned int ms) noexcept |
Blocks the current thread for the specified number of milliseconds. More... | |
template<typename Rep , typename Period > | |
static Error | Sleep (const std::chrono::duration< Rep, Period > &sleep_duration) noexcept |
Blocks the current thread for the specified duration. More... | |
Thread | ( | Function && | func, |
Args &&... | args | ||
) |
Create a new thread object and associate it with a new thread of execution.
Default thread parameters will be used. See the Start() function for more information.
func | Callable object to execute in the new thread. |
args | Arguments to pass to func. |
std::runtime_error | if Start() returns an error code. |
joinable() == true
|
inline |
Create a new thread object which does not yet represent a thread, passing explicit parameters.
The thread is not running until Start() is called.
priority | Priority of the thread. |
stack_size | Stack size of the thread in bytes. |
name | A name for the thread, maximum length ETCPAL_THREAD_NAME_MAX_LENGTH. |
platform_data | Pointer to a platform-specific parameter structure. See EtcPalThreadParams for more information. |
|
inlinevirtual |
Destroy the thread object.
If *this has a valid associated thread (joinable() == true
), the thread is joined before the destructor finishes.
Move another thread into this thread.
If *this has a valid associated thread (joinable() == true
), the behavior is undefined. After this call, *this has the parameters of other, and other is set to a default-constructed state.
|
inlinenoexcept |
Wait for the thread to finish execution.
If timeout_ms is not given, or if ETCPAL_THREAD_HAS_TIMED_JOIN is false on this platform, blocks until the thread has exited. Otherwise, blocks up to timeout_ms waiting for the thread to exit.
timeout_ms | How long to wait for the thread to exit (default forever). |
joinable() == false
). Move another thread into this thread.
If *this has a valid associated thread (joinable() == true
), the behavior is undefined. After this call, *this has the parameters of other, and other is set to a default-constructed state.
|
inlinenoexcept |
Get the native OS handle of this thread.
joinable() == false
).
|
inlinenoexcept |
Set the name of this thread.
This function does not have any effect on the associated thread unless it is called on a default-constructed thread before Start() is called. The pointer passed to this function must remain valid until after Start() is called.
name | Name to set. |
|
inlinenoexcept |
Set the name of this thread.
This function does not have any effect on the associated thread unless it is called on a default-constructed thread before Start() is called. The string reference passed to this function must remain valid until after Start() is called.
name | Name to set. |
|
inlinenoexcept |
Set this thread's parameters from an existing EtcPalThreadParams struct.
This function does not have any effect on the associated thread unless it is called on a default-constructed thread before Start() is called.
params | EtcPalThreadParams to use when starting the thread. |
|
inlinenoexcept |
Set the platform-specific parameter data.
This function does not have any effect on the associated thread unless it is called on a default-constructed thread before Start() is called. The pointer passed to this function must remain valid until after Start() is called.
platform_data | Pointer to platform-specific data structure. |
|
inlinenoexcept |
Set the priority of this thread.
Priority is not valid on all platforms. This function does not have any effect on the associated thread unless it is called on a default-constructed thread before Start() is called.
priority | Priority to set. |
|
inlinenoexcept |
Set the stack size of this thread in bytes.
Stack size is not valid on all platforms. This function does not have any effect on the associated thread unless it is called on a default-constructed thread before Start() is called.
stack_size | Stack size to set. |
|
staticnoexcept |
Blocks the current thread for the specified duration.
Note: Duration will be clamped to [0, UINT_MAX] milliseconds.
|
inlinestaticnoexcept |
Blocks the current thread for the specified number of milliseconds.
Error Start | ( | Function && | func, |
Args &&... | args | ||
) |
Associate this thread object with a new thread of execution.
The new thread of execution starts executing
where decay_copy is defined as
Except that the calls to decay_copy are evaluated in the context of the caller, so that any exceptions thrown during evaluation and copying/moving of the arguments are thrown in the current thread, without starting the new thread.
func | Callable object to execute in the new thread. |
args | Arguments to pass to func. |
joinable()
is now true. joinable() == true
).
|
inlinenoexcept |
Forcefully kill the thread.
Be careful when using this function. Depending on the state of a thread when it is terminated, shared resources or memory could not be freed, resulting in memory leaks or deadlocks.
joinable() == false
).