Important changes to repositories hosted on mbed.com
Mbed hosted mercurial repositories are deprecated and are due to be permanently deleted in July 2026.
To keep a copy of this software download the repository Zip archive or clone locally using Mercurial.
It is also possible to export all your personal repositories from the account settings page.
Thread Class Reference
[Thread class]
The Thread class allow defining, creating, and controlling thread functions in the system. More...
#include <Thread.h>
Inherits NonCopyable< Thread >.
Public Types | |
enum | State { Inactive, Ready, Running, WaitingDelay, WaitingJoin, WaitingThreadFlag, WaitingEventFlag, WaitingMutex, WaitingSemaphore, WaitingMemoryPool, WaitingMessageGet, WaitingMessagePut, WaitingInterval, WaitingOr, WaitingAnd, WaitingMailbox, Deleted } |
State of the Thread. More... | |
Public Member Functions | |
Thread (osPriority priority=osPriorityNormal, uint32_t stack_size=OS_STACK_SIZE, unsigned char *stack_mem=NULL, const char *name=NULL) | |
Allocate a new thread without starting execution. | |
MBED_DEPRECATED_SINCE ("mbed-os-5.1","Thread-spawning constructors hide errors. ""Replaced by thread.start(task).") Thread(mbed | |
Create a new thread, and start it executing the specified function. | |
template<typename T > | |
MBED_DEPRECATED_SINCE ("mbed-os-5.1","Thread-spawning constructors hide errors. ""Replaced by thread.start(callback(task, argument)).") Thread(T *argument | |
Create a new thread, and start it executing the specified function. | |
template<typename T , typename M > | |
MBED_DEPRECATED_SINCE ("mbed-os-5.1","The start function does not support cv-qualifiers. ""Replaced by thread.start(callback(obj, method)).") osStatus start(T *obj | |
Starts a thread executing the specified function. | |
osStatus | join () |
Wait for thread to terminate. | |
osStatus | terminate () |
Terminate execution of a thread and remove it from Active Threads. | |
osStatus | set_priority (osPriority priority) |
Set priority of an active thread. | |
osPriority | get_priority () |
Get priority of an active thread. | |
int32_t | signal_set (int32_t signals) |
Set the specified Thread Flags for the thread. | |
State | get_state () |
State of this Thread. | |
uint32_t | stack_size () |
Get the total stack memory size for this Thread. | |
uint32_t | free_stack () |
Get the currently unused stack memory for this Thread. | |
uint32_t | used_stack () |
Get the currently used stack memory for this Thread. | |
uint32_t | max_stack () |
Get the maximum stack memory usage to date for this Thread. | |
const char * | get_name () |
Get thread name. | |
virtual | ~Thread () |
Thread destructor. | |
Static Public Member Functions | |
static int32_t | signal_clr (int32_t signals) |
Clears the specified Thread Flags of the currently running thread. | |
static osEvent | signal_wait (int32_t signals, uint32_t millisec=osWaitForever) |
Wait for one or more Thread Flags to become signaled for the current RUNNING thread. | |
static osStatus | wait (uint32_t millisec) |
Wait for a specified time period in milliseconds Being tick-based, the delay will be up to the specified time - eg for a value of 1 the system waits until the next millisecond tick occurs, leading to a delay of 0-1 milliseconds. | |
static osStatus | wait_until (uint64_t millisec) |
Wait until a specified time in millisec The specified time is according to Kernel::get_ms_count(). | |
static osStatus | yield () |
Pass control to next thread that is in state READY. | |
static osThreadId | gettid () |
Get the thread id of the current running thread. | |
static void | attach_idle_hook (void(*fptr)(void)) |
Attach a function to be called by the RTOS idle task. | |
static void | attach_terminate_hook (void(*fptr)(osThreadId id)) |
Attach a function to be called when a task is killed. | |
Private Member Functions | |
MBED_DEPRECATED ("Invalid copy construction of a NonCopyable resource.") NonCopyable(const NonCopyable &) | |
NonCopyable copy constructor. | |
MBED_DEPRECATED ("Invalid copy assignment of a NonCopyable resource.") NonCopyable &operator | |
NonCopyable copy assignment operator. |
Detailed Description
The Thread class allow defining, creating, and controlling thread functions in the system.
Example:
#include "mbed.h" #include "rtos.h" Thread thread; DigitalOut led1(LED1); volatile bool running = true; // Blink function toggles the led in a long running loop void blink(DigitalOut *led) { while (running) { *led = !*led; wait(1); } } // Spawns a thread to run blink for 5 seconds int main() { thread.start(callback(blink, &led1)); wait(5); running = false; thread.join(); }
- Note:
- Memory considerations: The thread control structures will be created on current thread's stack, both for the mbed OS and underlying RTOS objects (static or dynamic RTOS memory pools are not being used). Additionally the stack memory for this thread will be allocated on the heap, if it wasn't supplied to the constructor.
Definition at line 76 of file Thread.h.
Member Enumeration Documentation
enum State |
State of the Thread.
- Enumerator:
Inactive NOT USED.
Ready Ready to run.
Running Running.
WaitingDelay Waiting for a delay to occur.
WaitingJoin Waiting for thread to join.
Only happens when using RTX directly.
WaitingThreadFlag Waiting for a thread flag to be set.
WaitingEventFlag Waiting for a event flag to be set.
WaitingMutex Waiting for a mutex event to occur.
WaitingSemaphore Waiting for a semaphore event to occur.
WaitingMemoryPool Waiting for a memory pool.
WaitingMessageGet Waiting for message to arrive.
WaitingMessagePut Waiting for message to be send.
WaitingInterval NOT USED.
WaitingOr NOT USED.
WaitingAnd NOT USED.
WaitingMailbox NOT USED (Mail is implemented as MemoryPool and Queue)
Deleted The task has been deleted or not started.
Constructor & Destructor Documentation
Thread | ( | osPriority | priority = osPriorityNormal , |
uint32_t | stack_size = OS_STACK_SIZE , |
||
unsigned char * | stack_mem = NULL , |
||
const char * | name = NULL |
||
) |
Allocate a new thread without starting execution.
- Parameters:
-
priority initial priority of the thread function. (default: osPriorityNormal). stack_size stack size (in bytes) requirements for the thread function. (default: OS_STACK_SIZE). stack_mem pointer to the stack area to be used by this thread (default: NULL). name name to be used for this thread. It has to stay allocated for the lifetime of the thread (default: NULL)
- Note:
- You cannot call this function from ISR context.
~Thread | ( | ) | [virtual] |
Thread destructor.
- Note:
- You cannot call this function from ISR context.
Definition at line 401 of file Thread.cpp.
Member Function Documentation
void attach_idle_hook | ( | void(*)(void) | fptr ) | [static] |
Attach a function to be called by the RTOS idle task.
- Parameters:
-
fptr pointer to the function to be called
- Note:
- You may call this function from ISR context.
Definition at line 393 of file Thread.cpp.
static void attach_terminate_hook | ( | void(*)(osThreadId id) | fptr ) | [static] |
Attach a function to be called when a task is killed.
- Parameters:
-
fptr pointer to the function to be called
- Note:
- You may call this function from ISR context.
uint32_t free_stack | ( | ) |
Get the currently unused stack memory for this Thread.
- Returns:
- the currently unused stack memory in bytes
- Note:
- You cannot call this function from ISR context.
Definition at line 261 of file Thread.cpp.
const char * get_name | ( | ) |
Get thread name.
- Returns:
- thread name or NULL if the name was not set.
- Note:
- You may call this function from ISR context.
Definition at line 311 of file Thread.cpp.
osPriority get_priority | ( | ) |
Get priority of an active thread.
- Returns:
- current priority value of the thread function.
- Note:
- You cannot call this function from ISR context.
Definition at line 170 of file Thread.cpp.
Thread::State get_state | ( | ) |
State of this Thread.
- Returns:
- the State of this Thread
- Note:
- You cannot call this function from ISR context.
Definition at line 184 of file Thread.cpp.
osThreadId gettid | ( | ) | [static] |
Get the thread id of the current running thread.
- Returns:
- thread ID for reference by other functions or NULL in case of error.
- Note:
- You may call this function from ISR context.
Definition at line 389 of file Thread.cpp.
osStatus join | ( | ) |
Wait for thread to terminate.
- Returns:
- status code that indicates the execution status of the function.
- Note:
- not callable from interrupt
- You cannot call this function from ISR context.
Definition at line 142 of file Thread.cpp.
uint32_t max_stack | ( | ) |
Get the maximum stack memory usage to date for this Thread.
- Returns:
- the maximum stack memory usage to date in bytes
- Note:
- You cannot call this function from ISR context.
Definition at line 291 of file Thread.cpp.
MBED_DEPRECATED_SINCE | ( | "mbed-os-5.1" | , |
"Thread-spawning constructors hide errors. ""Replaced by thread.start(task)." | |||
) |
Create a new thread, and start it executing the specified function.
- Parameters:
-
task function to be executed by this thread. priority initial priority of the thread function. (default: osPriorityNormal). stack_size stack size (in bytes) requirements for the thread function. (default: OS_STACK_SIZE). stack_mem pointer to the stack area to be used by this thread (default: NULL).
Thread thread(priority, stack_size, stack_mem); osStatus status = thread.start(task); if (status != osOK) { error("oh no!"); }
- Note:
- You cannot call this function from ISR context.
MBED_DEPRECATED_SINCE | ( | "mbed-os-5.1" | , |
"The start function does not support cv-qualifiers. ""Replaced by thread.start(callback(obj, method))." | |||
) |
Starts a thread executing the specified function.
- Parameters:
-
obj argument to task method function to be executed by this thread.
- Returns:
- status code that indicates the execution status of the function.
- Note:
- You cannot call this function from ISR context.
MBED_DEPRECATED_SINCE | ( | "mbed-os-5.1" | , |
"Thread-spawning constructors hide errors. ""Replaced by thread.start(callback(task, argument))." | |||
) |
Create a new thread, and start it executing the specified function.
- Parameters:
-
argument pointer that is passed to the thread function as start argument. (default: NULL). task argument to task. priority initial priority of the thread function. (default: osPriorityNormal). stack_size stack size (in bytes) requirements for the thread function. (default: OS_STACK_SIZE). stack_mem pointer to the stack area to be used by this thread (default: NULL).
Thread thread(priority, stack_size, stack_mem); osStatus status = thread.start(callback(task, argument)); if (status != osOK) { error("oh no!"); }
- Note:
- You cannot call this function from ISR context.
osStatus set_priority | ( | osPriority | priority ) |
Set priority of an active thread.
- Parameters:
-
priority new priority value for the thread function.
- Returns:
- status code that indicates the execution status of the function.
- Note:
- You cannot call this function from ISR context.
Definition at line 160 of file Thread.cpp.
int32_t signal_clr | ( | int32_t | signals ) | [static] |
Clears the specified Thread Flags of the currently running thread.
- Parameters:
-
signals specifies the signal flags of the thread that should be cleared.
- Returns:
- signal flags before clearing or osFlagsError in case of incorrect parameters.
- Note:
- You cannot call this function from ISR context.
Definition at line 315 of file Thread.cpp.
int32_t signal_set | ( | int32_t | signals ) |
Set the specified Thread Flags for the thread.
- Parameters:
-
signals specifies the signal flags of the thread that should be set.
- Returns:
- signal flags after setting or osFlagsError in case of incorrect parameters.
- Note:
- You may call this function from ISR context.
Definition at line 180 of file Thread.cpp.
osEvent signal_wait | ( | int32_t | signals, |
uint32_t | millisec = osWaitForever |
||
) | [static] |
Wait for one or more Thread Flags to become signaled for the current RUNNING thread.
- Parameters:
-
signals wait until all specified signal flags are set or 0 for any single signal flag. millisec timeout value or 0 in case of no time-out. (default: osWaitForever).
- Returns:
- event flag information or error code.
- Note:
- if millisec is set to 0 and flag is no set the event carries osOK value.
- You cannot call this function from ISR context.
Definition at line 319 of file Thread.cpp.
uint32_t stack_size | ( | ) |
Get the total stack memory size for this Thread.
- Returns:
- the total stack memory size in bytes
- Note:
- You cannot call this function from ISR context.
osStatus terminate | ( | void | ) |
Terminate execution of a thread and remove it from Active Threads.
- Returns:
- status code that indicates the execution status of the function.
- Note:
- You cannot call this function from ISR context.
Definition at line 120 of file Thread.cpp.
uint32_t used_stack | ( | ) |
Get the currently used stack memory for this Thread.
- Returns:
- the currently used stack memory in bytes
- Note:
- You cannot call this function from ISR context.
Definition at line 276 of file Thread.cpp.
osStatus wait | ( | uint32_t | millisec ) | [static] |
Wait for a specified time period in milliseconds Being tick-based, the delay will be up to the specified time - eg for a value of 1 the system waits until the next millisecond tick occurs, leading to a delay of 0-1 milliseconds.
- Parameters:
-
millisec time delay value
- Returns:
- status code that indicates the execution status of the function.
- Note:
- You cannot call this function from ISR context.
Definition at line 352 of file Thread.cpp.
osStatus wait_until | ( | uint64_t | millisec ) | [static] |
Wait until a specified time in millisec The specified time is according to Kernel::get_ms_count().
- Parameters:
-
millisec absolute time in millisec
- Returns:
- status code that indicates the execution status of the function.
- Note:
- not callable from interrupt
- if millisec is equal to or lower than the current tick count, this returns immediately, either with an error or "osOK".
- the underlying RTOS may have a limit to the maximum wait time due to internal 32-bit computations, but this is guaranteed to work if the delay is <= 0x7fffffff milliseconds (~24 days). If the limit is exceeded, it may return with an immediate error, or wait for the maximum delay.
- You cannot call this function from ISR context.
Definition at line 356 of file Thread.cpp.
osStatus yield | ( | ) | [static] |
Pass control to next thread that is in state READY.
- Returns:
- status code that indicates the execution status of the function.
- Note:
- You cannot call this function from ISR context.
Definition at line 385 of file Thread.cpp.
Generated on Tue Jul 12 2022 14:27:16 by
