Mbed OS Reference
Loading...
Searching...
No Matches
Thread Class Reference

The Thread class allow defining, creating, and controlling thread functions in the system. More...

#include <Thread.h>

Inheritance diagram for Thread:
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=nullptr, const char *name=nullptr)
 Allocate a new thread without starting execution. More...
 
 Thread (uint32_t tz_module, osPriority priority=osPriorityNormal, uint32_t stack_size=OS_STACK_SIZE, unsigned char *stack_mem=nullptr, const char *name=nullptr)
 Allocate a new thread without starting execution. More...
 
osStatus start (mbed::Callback< void()> task)
 Starts a thread executing the specified function. More...
 
osStatus join ()
 Wait for thread to terminate. More...
 
osStatus terminate ()
 Terminate execution of a thread and remove it from Active Threads. More...
 
osStatus set_priority (osPriority priority)
 Set priority of an active thread. More...
 
osPriority get_priority () const
 Get priority of an active thread. More...
 
uint32_t flags_set (uint32_t flags)
 Set the specified Thread Flags for the thread. More...
 
State get_state () const
 State of this Thread. More...
 
uint32_t stack_size () const
 Get the total stack memory size for this Thread. More...
 
uint32_t free_stack () const
 Get the currently unused stack memory for this Thread. More...
 
uint32_t used_stack () const
 Get the currently used stack memory for this Thread. More...
 
uint32_t max_stack () const
 Get the maximum stack memory usage to date for this Thread. More...
 
const char * get_name () const
 Get thread name. More...
 
osThreadId_t get_id () const
 Get thread id. More...
 
virtual ~Thread ()
 Thread destructor. More...
 

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;
}
}
// Spawns a thread to run blink for 5 seconds
int main() {
thread.start(callback(blink, &led1));
running = false;
thread.join();
}
The Thread class allow defining, creating, and controlling thread functions in the system.
Definition: Thread.h:92
osStatus join()
Wait for thread to terminate.
osStatus start(mbed::Callback< void()> task)
Starts a thread executing the specified function.
void sleep_for(uint32_t millisec)
Sleep for a specified time period in millisec:
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.
MBED_TZ_DEFAULT_ACCESS (default:0) flag can be used to change the default access of all user threads in non-secure mode. MBED_TZ_DEFAULT_ACCESS set to 1, means all non-secure user threads have access to call secure functions. MBED_TZ_DEFAULT_ACCESS set to 0, means none of the non-secure user thread have access to call secure functions, to give access to particular thread used overloaded constructor with tz_module as argument during thread creation.

MBED_TZ_DEFAULT_ACCESS is target specific define, should be set in targets.json file for Cortex-M23/M33 devices.

Note
Bare metal profile: This class is not supported.

Definition at line 92 of file Thread.h.

Member Enumeration Documentation

◆ State

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.

Definition at line 180 of file Thread.h.

Constructor & Destructor Documentation

◆ Thread() [1/2]

Thread ( osPriority  priority = osPriorityNormal,
uint32_t  stack_size = OS_STACK_SIZE,
unsigned char *  stack_mem = nullptr,
const char *  name = nullptr 
)

Allocate a new thread without starting execution.

Parameters
priorityinitial priority of the thread function. (default: osPriorityNormal).
stack_sizestack size (in bytes) requirements for the thread function. (default: OS_STACK_SIZE).
stack_mempointer to the stack area to be used by this thread (default: nullptr).
namename to be used for this thread. It has to stay allocated for the lifetime of the thread (default: nullptr)
Note
Default value of tz_module will be MBED_TZ_DEFAULT_ACCESS
You cannot call this function from ISR context.

Definition at line 104 of file Thread.h.

◆ Thread() [2/2]

Thread ( uint32_t  tz_module,
osPriority  priority = osPriorityNormal,
uint32_t  stack_size = OS_STACK_SIZE,
unsigned char *  stack_mem = nullptr,
const char *  name = nullptr 
)

Allocate a new thread without starting execution.

Parameters
tz_moduletrustzone thread identifier (osThreadAttr_t::tz_module) Context of RTOS threads in non-secure state must be saved when calling secure functions. tz_module ID is used to allocate context memory for threads, and it can be safely set to zero for threads not using secure calls at all. See "TrustZone RTOS Context Management" for more details.
priorityinitial priority of the thread function. (default: osPriorityNormal).
stack_sizestack size (in bytes) requirements for the thread function. (default: OS_STACK_SIZE).
stack_mempointer to the stack area to be used by this thread (default: nullptr).
namename to be used for this thread. It has to stay allocated for the lifetime of the thread (default: nullptr)
Note
You cannot call this function from ISR context.

Definition at line 124 of file Thread.h.

◆ ~Thread()

virtual ~Thread ( )
virtual

Thread destructor.

Note
You cannot call this function from ISR context.

Member Function Documentation

◆ start()

osStatus start ( mbed::Callback< void()>  task)

Starts a thread executing the specified function.

Parameters
taskfunction to be executed by this thread.
Returns
status code that indicates the execution status of the function, or osErrorNoMemory if stack allocation failed.
Note
a thread can only be started once
You cannot call this function ISR context.

◆ join()

osStatus join ( )

Wait for thread to terminate.

Returns
status code that indicates the execution status of the function.
Note
You cannot call this function from ISR context.

◆ terminate()

osStatus terminate ( )

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.

◆ set_priority()

osStatus set_priority ( osPriority  priority)

Set priority of an active thread.

Parameters
prioritynew 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.

◆ get_priority()

osPriority get_priority ( ) const

Get priority of an active thread.

Returns
current priority value of the thread function.
Note
You cannot call this function from ISR context.

◆ flags_set()

uint32_t flags_set ( uint32_t  flags)

Set the specified Thread Flags for the thread.

Parameters
flagsspecifies the flags of the thread that should be set.
Returns
thread flags after setting or osFlagsError in case of incorrect parameters.
Note
You may call this function from ISR context.

◆ get_state()

State get_state ( ) const

State of this Thread.

Returns
the State of this Thread
Note
You cannot call this function from ISR context.

◆ stack_size()

uint32_t stack_size ( ) const

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.

◆ free_stack()

uint32_t free_stack ( ) const

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.

◆ used_stack()

uint32_t used_stack ( ) const

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.

◆ max_stack()

uint32_t max_stack ( ) const

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.

◆ get_name()

const char * get_name ( ) const

Get thread name.

Returns
thread name or nullptr if the name was not set.
Note
You may call this function from ISR context.

◆ get_id()

osThreadId_t get_id ( ) const

Get thread id.

Returns
thread ID for reference by other functions.
Note
You may call this function from ISR context.