Rtos API example
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. | |
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 millisec: | |
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)
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
Definition at line 360 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
uint32_t free_stack | ( | ) |
Get the currently unused stack memory for this Thread.
- Returns:
- the currently unused stack memory in bytes
Definition at line 257 of file Thread.cpp.
const char * get_name | ( | ) |
Get thread name.
- Returns:
- thread name or NULL if the name was not set.
Definition at line 307 of file Thread.cpp.
osPriority get_priority | ( | ) |
Get priority of an active thread.
- Returns:
- current priority value of the thread function.
Definition at line 166 of file Thread.cpp.
Thread::State get_state | ( | ) |
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.
Definition at line 356 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
Definition at line 138 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
Definition at line 287 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!"); }
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.
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!"); }
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.
Definition at line 156 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.
Definition at line 311 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.
Definition at line 176 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.
- not callable from interrupt
Definition at line 315 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
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.
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
Definition at line 272 of file Thread.cpp.
osStatus wait | ( | uint32_t | millisec ) | [static] |
Wait for a specified time period in millisec:
- Parameters:
-
millisec time delay value
- Returns:
- status code that indicates the execution status of the function.
- Note:
- not callable from interrupt
Definition at line 348 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:
- not callable from interrupt
Definition at line 352 of file Thread.cpp.
Generated on Sun Jul 17 2022 08:25:44 by 1.7.2