CMSIS-RTOS2  Version 2.1.3
Real-Time Operating System: API and RTX Reference Implementation
 All Data Structures Files Functions Variables Typedefs Enumerations Enumerator Macros Groups Pages
Thread Management

Define, create, and control thread functions. More...

Data Structures

struct  osThreadAttr_t
 Attributes structure for thread. More...
 

Macros

#define osThreadJoinable   0x00000001U
 Thread created in joinable mode. More...
 
#define osThreadDetached   0x00000000U
 Thread created in detached mode (default) More...
 

Typedefs

typedef void(* osThreadFunc_t )(void *argument)
 Entry point of a thread. More...
 
typedef void * osThreadId_t
 

Enumerations

enum  osThreadState_t {
  osThreadInactive = 0,
  osThreadReady = 1,
  osThreadRunning = 2,
  osThreadBlocked = 3,
  osThreadTerminated = 4,
  osThreadError = -1,
  osThreadReserved = 0x7FFFFFFF
}
 Thread state. More...
 
enum  osPriority_t {
  osPriorityNone = 0,
  osPriorityIdle = 1,
  osPriorityLow = 8,
  osPriorityLow1 = 8+1,
  osPriorityLow2 = 8+2,
  osPriorityLow3 = 8+3,
  osPriorityLow4 = 8+4,
  osPriorityLow5 = 8+5,
  osPriorityLow6 = 8+6,
  osPriorityLow7 = 8+7,
  osPriorityBelowNormal = 16,
  osPriorityBelowNormal1 = 16+1,
  osPriorityBelowNormal2 = 16+2,
  osPriorityBelowNormal3 = 16+3,
  osPriorityBelowNormal4 = 16+4,
  osPriorityBelowNormal5 = 16+5,
  osPriorityBelowNormal6 = 16+6,
  osPriorityBelowNormal7 = 16+7,
  osPriorityNormal = 24,
  osPriorityNormal1 = 24+1,
  osPriorityNormal2 = 24+2,
  osPriorityNormal3 = 24+3,
  osPriorityNormal4 = 24+4,
  osPriorityNormal5 = 24+5,
  osPriorityNormal6 = 24+6,
  osPriorityNormal7 = 24+7,
  osPriorityAboveNormal = 32,
  osPriorityAboveNormal1 = 32+1,
  osPriorityAboveNormal2 = 32+2,
  osPriorityAboveNormal3 = 32+3,
  osPriorityAboveNormal4 = 32+4,
  osPriorityAboveNormal5 = 32+5,
  osPriorityAboveNormal6 = 32+6,
  osPriorityAboveNormal7 = 32+7,
  osPriorityHigh = 40,
  osPriorityHigh1 = 40+1,
  osPriorityHigh2 = 40+2,
  osPriorityHigh3 = 40+3,
  osPriorityHigh4 = 40+4,
  osPriorityHigh5 = 40+5,
  osPriorityHigh6 = 40+6,
  osPriorityHigh7 = 40+7,
  osPriorityRealtime = 48,
  osPriorityRealtime1 = 48+1,
  osPriorityRealtime2 = 48+2,
  osPriorityRealtime3 = 48+3,
  osPriorityRealtime4 = 48+4,
  osPriorityRealtime5 = 48+5,
  osPriorityRealtime6 = 48+6,
  osPriorityRealtime7 = 48+7,
  osPriorityISR = 56,
  osPriorityError = -1,
  osPriorityReserved = 0x7FFFFFFF
}
 Priority values. More...
 

Functions

osThreadId_t osThreadNew (osThreadFunc_t func, void *argument, const osThreadAttr_t *attr)
 Create a thread and add it to Active Threads. More...
 
const char * osThreadGetName (osThreadId_t thread_id)
 Get name of a thread. More...
 
osThreadId_t osThreadGetId (void)
 Return the thread ID of the current running thread. More...
 
osThreadState_t osThreadGetState (osThreadId_t thread_id)
 Get current thread state of a thread. More...
 
osStatus_t osThreadSetPriority (osThreadId_t thread_id, osPriority_t priority)
 Change priority of a thread. More...
 
osPriority_t osThreadGetPriority (osThreadId_t thread_id)
 Get current priority of a thread. More...
 
osStatus_t osThreadYield (void)
 Pass control to next thread that is in state READY. More...
 
osStatus_t osThreadSuspend (osThreadId_t thread_id)
 Suspend execution of a thread. More...
 
osStatus_t osThreadResume (osThreadId_t thread_id)
 Resume execution of a thread. More...
 
osStatus_t osThreadDetach (osThreadId_t thread_id)
 Detach a thread (thread storage can be reclaimed when thread terminates). More...
 
osStatus_t osThreadJoin (osThreadId_t thread_id)
 Wait for specified thread to terminate. More...
 
__NO_RETURN void osThreadExit (void)
 Terminate execution of current running thread. More...
 
osStatus_t osThreadTerminate (osThreadId_t thread_id)
 Terminate execution of a thread. More...
 
uint32_t osThreadGetStackSize (osThreadId_t thread_id)
 Get stack size of a thread. More...
 
uint32_t osThreadGetStackSpace (osThreadId_t thread_id)
 Get available stack space of a thread based on stack watermark recording during execution. More...
 
uint32_t osThreadGetCount (void)
 Get number of active threads. More...
 
uint32_t osThreadEnumerate (osThreadId_t *thread_array, uint32_t array_items)
 Enumerate active threads. More...
 

Description

The Thread Management function group allows defining, creating, and controlling thread functions in the system.

Note
Thread management functions cannot be called from Interrupt Service Routines.

Thread states

Threads can be in the following states:

ThreadStatus.png
Thread State and State Transitions

A CMSIS-RTOS assumes that threads are scheduled as shown in the figure Thread State and State Transitions. The thread states change as follows:

Note
Refer to Thread Configuration for RTX5 configuration options.

Thread Examples

The following examples show various scenarios to create threads:

Example 1 - Create a simple thread

Create a thread out of the function thread1 using all default values for thread attributes and memory from the Global Memory Pool.

__NO_RETURN void thread1 (void *argument) {
// ...
for (;;) {}
}
int main (void) {
;
osThreadNew(thread1, NULL, NULL); // Create thread with default settings
;
}

Example 2 - Create thread with stack non-default stack size

Similar to the simple thread all attributes are default. The stack is dynamically allocated from the Global Memory Pool

osThreadAttr_t::stack_size is used to pass the stack size in Bytes to osThreadNew.

__NO_RETURN void thread1 (void *argument) {
// ...
for (;;) {}
}
const osThreadAttr_t thread1_attr = {
.stack_size = 1024 // Create the thread stack with a size of 1024 bytes
};
int main (void) {
;
osThreadNew(thread1, NULL, &thread1_attr); // Create thread with custom sized stack memory
;
}

Example 3 - Create thread with statically allocated stack

Similar to the simple thread all attributes are default. The stack is statically allocated using the uint64_t array thread1_stk_1. This allocates 64*8 Bytes (=512 Bytes) with an alignment of 8 Bytes (mandatory for Cortex-M stack memory).

osThreadAttr_t::stack_mem holds a pointer to the stacks lowest address.

osThreadAttr_t::stack_size is used to pass the stack size in Bytes to osThreadNew.

__NO_RETURN void thread1 (void *argument) {
// ...
for (;;) {}
}
static uint64_t thread1_stk_1[64];
const osThreadAttr_t thread1_attr = {
.stack_mem = &thread1_stk_1[0],
.stack_size = sizeof(thread1_stk_1)
};
int main (void) {
;
osThreadNew(thread1, NULL, &thread1_attr); // Create thread with statically allocated stack memory
;
}

Example 4 - Thread with statically allocated task control block

Typically this method is chosen together with a statically allocated stack as shown in Example 2.

#include "cmsis_os2.h"
//include rtx_os.h for types of RTX objects
#include "rtx_os.h"
__NO_RETURN void thread1 (void *argument) {
// ...
for (;;) {}
}
static osRtxThread_t thread1_tcb;
const osThreadAttr_t thread1_attr = {
.cb_mem = &thread1_tcb,
.cb_size = sizeof(thread1_tcb),
};
int main (void) {
;
osThreadNew(thread1, NULL, &thread1_attr); // Create thread with custom tcb memory
;
}

Example 5 - Create thread with a different priority

The default priority of RTX is osPriorityNormal. Often you want to run a task with a higher or lower priority. Using the osThreadAttr_t control structure you can set any initial priority required.

__NO_RETURN void thread1 (void *argument) {
// ...
for (;;) {}
}
const osThreadAttr_t thread1_attr = {
.priority = osPriorityHigh //Set initial thread priority to high
};
int main (void) {
;
osThreadNew(thread1, NULL, &thread1_attr);
;
}

Example 6 - Joinable threads

In this example a master thread creates four threads with the osThreadJoinable attribute. These will do some work and return using the osThreadExit call after finished. osThreadJoin is used to synchronize the thread termination.

__NO_RETURN void worker (void *argument) {
; // work a lot on data[]
osDelay(1000U);
}
__NO_RETURN void thread1 (void *argument) {
osThreadAttr_t worker_attr;
osThreadId_t worker_ids[4];
uint8_t data[4][10];
memset(&worker_attr, 0, sizeof(worker_attr));
worker_attr.attr_bits = osThreadJoinable;
worker_ids[0] = osThreadNew(worker, &data[0][0], &worker_attr);
worker_ids[1] = osThreadNew(worker, &data[1][0], &worker_attr);
worker_ids[2] = osThreadNew(worker, &data[2][0], &worker_attr);
worker_ids[3] = osThreadNew(worker, &data[3][0], &worker_attr);
osThreadJoin(worker_ids[0]);
osThreadJoin(worker_ids[1]);
osThreadJoin(worker_ids[2]);
osThreadJoin(worker_ids[3]);
}

Data Structure Documentation

struct osThreadAttr_t

Specifies the following attributes for the osThreadNew function.

Data Fields
const char * name name of the thread

Pointer to a constant string with a human readable name (displayed during debugging) of the thread object.

Default: NULL no name specified (debugger may display function name instead).

uint32_t attr_bits attribute bits

The following bit masks can be used to set options:

void * cb_mem memory for control block

Pointer to a memory for the thread control block object. Refer to Static Object Memory for more information.

Default: NULL to use Automatic Dynamic Allocation for the thread control block.

uint32_t cb_size size of provided memory for control block

The size (in bytes) of memory block passed with cb_mem. For RTX, the minimum value is defined with osRtxThreadCbSize (higher values are permitted).

Default: 0 as the default is no memory provided with cb_mem.

void * stack_mem memory for stack

Pointer to a memory location for the thread stack (64-bit aligned).

Default: NULL to allocate stack from a fixed-size memory pool using Thread Stack Management.

uint32_t stack_size size of stack

The size (in bytes) of the stack specified by stack_mem.

Default: 0 as the default is no memory provided with stack_mem.

/**

osPriority_t priority initial thread priority (default: osPriorityNormal)

Specifies the initial thread priority with a value from osPriority_t.

Default: osPriorityNormal.

TZ_ModuleId_t tz_module TrustZone module identifier.

TrustZone Thread Context Management Identifier to allocate context memory for threads. The RTOS kernel that runs in non-secure state calls the interface functions defined by the header file TZ_context.h. Can safely be set to zero for threads not using secure calls at all. See TrustZone RTOS Context Management.

Default: 0 not thread context specified.

/**

uint32_t reserved reserved (must be 0)

Reserved for future use.

Macro Definition Documentation

#define osThreadJoinable   0x00000001U

A thread in this state can be joined using osThreadJoin.

#define osThreadDetached   0x00000000U

A thread in this state cannot be joined using osThreadJoin.

Typedef Documentation

void(* osThreadFunc_t)(void *argument)

Entry function for threads. Setting up a new thread (osThreadNew) will start execution with a call into this entry function. The optional argument can be used to hand over arbitrary user data to the thread, i.e. to identify the thread or for runtime parameters.

Parameters
[in]argumentArbitrary user data set on osThreadNew.

Thread ID identifies the thread.

Returned by:

Enumeration Type Documentation

State of a thread as retrieved by osThreadGetState. In case osThreadGetState fails or if it is called from an ISR, it will return osThreadError, otherwise it returns the thread state.

Enumerator
osThreadInactive 

Inactive.

The thread is created but not actively used, or has been terminated (returned for static control block allocation, when memory pools are used osThreadError is returned as the control block is no longer valid)

osThreadReady 

Ready.

The thread is ready for execution but not currently running.

osThreadRunning 

Running.

The thread is currently running.

osThreadBlocked 

Blocked.

The thread is currently blocked (delayed, waiting for an event or suspended).

osThreadTerminated 

Terminated.

The thread is terminated and all its resources are not yet freed (applies to joinable threads).

osThreadError 

Error.

The thread does not exist (has raised an error condition) and cannot be scheduled.

osThreadReserved 

Prevents enum down-size compiler optimization.

The osPriority_t value specifies the priority for a thread. The default thread priority should be osPriorityNormal. If an active thread becomes ready that has a higher priority than the currently running thread then a thread switch occurs immediately. The system continues executing the thread with the higher priority.

To prevent from a priority inversion, a CMSIS-RTOS compliant OS may optionally implement a priority inheritance method. A priority inversion occurs when a high priority thread is waiting for a resource or event that is controlled by a thread with a lower priority. Thus causing the high priority thread potentially beeing blocked forever by another thread with lower priority. To come over this issue the low priority thread controlling the resource should be treated as having the higher priority until it releases the resource.

Note
Priority inheritance only applies to mutexes.
Enumerator
osPriorityNone 

No priority (not initialized).

osPriorityIdle 

Reserved for Idle thread.

This lowest priority should not be used for any other thread.

osPriorityLow 

Priority: low.

osPriorityLow1 

Priority: low + 1.

osPriorityLow2 

Priority: low + 2.

osPriorityLow3 

Priority: low + 3.

osPriorityLow4 

Priority: low + 4.

osPriorityLow5 

Priority: low + 5.

osPriorityLow6 

Priority: low + 6.

osPriorityLow7 

Priority: low + 7.

osPriorityBelowNormal 

Priority: below normal.

osPriorityBelowNormal1 

Priority: below normal + 1.

osPriorityBelowNormal2 

Priority: below normal + 2.

osPriorityBelowNormal3 

Priority: below normal + 3.

osPriorityBelowNormal4 

Priority: below normal + 4.

osPriorityBelowNormal5 

Priority: below normal + 5.

osPriorityBelowNormal6 

Priority: below normal + 6.

osPriorityBelowNormal7 

Priority: below normal + 7.

osPriorityNormal 

Priority: normal.

osPriorityNormal1 

Priority: normal + 1.

osPriorityNormal2 

Priority: normal + 2.

osPriorityNormal3 

Priority: normal + 3.

osPriorityNormal4 

Priority: normal + 4.

osPriorityNormal5 

Priority: normal + 5.

osPriorityNormal6 

Priority: normal + 6.

osPriorityNormal7 

Priority: normal + 7.

osPriorityAboveNormal 

Priority: above normal.

osPriorityAboveNormal1 

Priority: above normal + 1.

osPriorityAboveNormal2 

Priority: above normal + 2.

osPriorityAboveNormal3 

Priority: above normal + 3.

osPriorityAboveNormal4 

Priority: above normal + 4.

osPriorityAboveNormal5 

Priority: above normal + 5.

osPriorityAboveNormal6 

Priority: above normal + 6.

osPriorityAboveNormal7 

Priority: above normal + 7.

osPriorityHigh 

Priority: high.

osPriorityHigh1 

Priority: high + 1.

osPriorityHigh2 

Priority: high + 2.

osPriorityHigh3 

Priority: high + 3.

osPriorityHigh4 

Priority: high + 4.

osPriorityHigh5 

Priority: high + 5.

osPriorityHigh6 

Priority: high + 6.

osPriorityHigh7 

Priority: high + 7.

osPriorityRealtime 

Priority: realtime.

osPriorityRealtime1 

Priority: realtime + 1.

osPriorityRealtime2 

Priority: realtime + 2.

osPriorityRealtime3 

Priority: realtime + 3.

osPriorityRealtime4 

Priority: realtime + 4.

osPriorityRealtime5 

Priority: realtime + 5.

osPriorityRealtime6 

Priority: realtime + 6.

osPriorityRealtime7 

Priority: realtime + 7.

osPriorityISR 

Reserved for ISR deferred thread.

This highest priority might be used by the RTOS implementation but must not be used for any user thread.

osPriorityError 

System cannot determine priority or illegal priority.

osPriorityReserved 

Prevents enum down-size compiler optimization.

Function Documentation

osThreadId_t osThreadNew ( osThreadFunc_t  func,
void *  argument,
const osThreadAttr_t attr 
)
Parameters
[in]functhread function.
[in]argumentpointer that is passed to the thread function as start argument.
[in]attrthread attributes; NULL: default values.
Returns
thread ID for reference by other functions or NULL in case of error.

The function osThreadNew starts a thread function by adding it to the list of active threads and sets it to state READY. Arguments for the thread function are passed using the parameter pointer *argument. When the priority of the created thread function is higher than the current RUNNING thread, the created thread function starts instantly and becomes the new RUNNING thread. Thread attributes are defined with the parameter pointer attr. Attributes include settings for thread priority, stack size, or memory allocation.

The function can be safely called before the RTOS is started (call to osKernelStart), but not before it is initialized (call to osKernelInitialize).

The function osThreadNew returns the pointer to the thread object identifier or NULL in case of an error.

Note
Cannot be called from Interrupt Service Routines.

Code Example

Refer to the Thread Examples section.

const char * osThreadGetName ( osThreadId_t  thread_id)
Parameters
[in]thread_idthread ID obtained by osThreadNew or osThreadGetId.
Returns
name as null-terminated string.

The function osThreadGetName returns the pointer to the name string of the thread identified by parameter thread_id or NULL in case of an error.

Note
This function cannot be called from Interrupt Service Routines.

Code Example

void ThreadGetName_example (void) {
osThreadId_t thread_id = osThreadGetId();
const char* name = osThreadGetName(thread_id);
if (name == NULL) {
// Failed to get the thread name; not in a thread
}
}
osThreadId_t osThreadGetId ( void  )
Returns
thread ID for reference by other functions or NULL in case of error.

The function osThreadGetId returns the thread object ID of the currently running thread or NULL in case of an error.

Note
This function may be called from Interrupt Service Routines.

Code Example

void ThreadGetId_example (void) {
osThreadId_t id; // id for the currently running thread
id = osThreadGetId();
if (id == NULL) {
// Failed to get the id
}
}
osThreadState_t osThreadGetState ( osThreadId_t  thread_id)
Parameters
[in]thread_idthread ID obtained by osThreadNew or osThreadGetId.
Returns
current thread state of the specified thread.

The function osThreadGetState returns the state of the thread identified by parameter thread_id. In case it fails or if it is called from an ISR, it will return osThreadError, otherwise it returns the thread state (refer to osThreadState_t for the list of thread states).

Note
This function cannot be called from Interrupt Service Routines.
osStatus_t osThreadSetPriority ( osThreadId_t  thread_id,
osPriority_t  priority 
)
Parameters
[in]thread_idthread ID obtained by osThreadNew or osThreadGetId.
[in]prioritynew priority value for the thread function.
Returns
status code that indicates the execution status of the function.

The function osThreadSetPriority changes the priority of an active thread specified by the parameter thread_id to the priority specified by the parameter priority.

Possible osStatus_t return values:

  • osOK: the priority of the specified thread has been changed successfully.
  • osErrorParameter: thread_id is NULL or invalid or priority is incorrect.
  • osErrorResource: the thread is in an invalid state.
  • osErrorISR: the function osThreadSetPriority cannot be called from interrupt service routines.
Note
This function cannot be called from Interrupt Service Routines.

Code Example

#include "cmsis_os2.h"
void Thread_1 (void const *arg) { // Thread function
osThreadId_t id; // id for the currently running thread
osStatus_t status; // status of the executed function
:
id = osThreadGetId(); // Obtain ID of current running thread
status = osThreadSetPriority(id, osPriorityBelowNormal); // Set thread priority
if (status == osOK) {
// Thread priority changed to BelowNormal
}
else {
// Failed to set the priority
}
:
}
osPriority_t osThreadGetPriority ( osThreadId_t  thread_id)
Parameters
[in]thread_idthread ID obtained by osThreadNew or osThreadGetId.
Returns
current priority value of the specified thread.

The function osThreadGetPriority returns the priority of an active thread specified by the parameter thread_id.

Possible osPriority_t return values:

  • priority: the priority of the specified thread.
  • osPriorityError: priority cannot be determined or is illegal. It is also returned when the function is called from Interrupt Service Routines.
Note
This function cannot be called from Interrupt Service Routines.

Code Example

#include "cmsis_os2.h"
void Thread_1 (void const *arg) { // Thread function
osThreadId_t id; // id for the currently running thread
osPriority_t priority; // thread priority
id = osThreadGetId(); // Obtain ID of current running thread
priority = osThreadGetPriority(id); // Obtain the thread priority
}
osStatus_t osThreadYield ( void  )
Returns
status code that indicates the execution status of the function.

The function osThreadYield passes control to the next thread with the same priority that is in the READY state. If there is no other thread with the same priority in state READY, then the current thread continues execution and no thread switch occurs. osThreadYield does not set the thread to state BLOCKED. Thus no thread with a lower priority will be scheduled even if threads in state READY are available.

Possible osStatus_t return values:

  • osOK: control has been passed to the next thread successfully.
  • osError: an unspecified error has occurred.
  • osErrorISR: the function osThreadYield cannot be called from interrupt service routines.
Note
This function cannot be called from Interrupt Service Routines.
This function has no impact when called when the kernel is locked, see osKernelLock.

Code Example

#include "cmsis_os2.h"
void Thread_1 (void const *arg) { // Thread function
osStatus_t status; // status of the executed function
:
while (1) {
status = osThreadYield();
if (status != osOK) {
// an error occurred
}
}
}
osStatus_t osThreadSuspend ( osThreadId_t  thread_id)
Parameters
[in]thread_idthread ID obtained by osThreadNew or osThreadGetId.
Returns
status code that indicates the execution status of the function.

The function osThreadSuspend suspends the execution of the thread identified by parameter thread_id. The thread is put into the BLOCKED state (osThreadBlocked). Suspending the running thread will cause a context switch to another thread in READY state immediately. The suspended thread is not executed until explicitly resumed with the function osThreadResume.

Threads that are already BLOCKED are removed from any wait list and become ready when they are resumed. Thus it is not recommended to suspend an already blocked thread.

Possible osStatus_t return values:

  • osOK: the thread has been suspended successfully.
  • osErrorParameter: thread_id is NULL or invalid.
  • osErrorResource: the thread is in an invalid state.
  • osErrorISR: the function osThreadSuspend cannot be called from interrupt service routines.
Note
This function cannot be called from Interrupt Service Routines.
This function must not be called to suspend the running thread when the kernel is locked, i.e. osKernelLock.
osStatus_t osThreadResume ( osThreadId_t  thread_id)
Parameters
[in]thread_idthread ID obtained by osThreadNew or osThreadGetId.
Returns
status code that indicates the execution status of the function.

The function osThreadResume puts the thread identified by parameter thread_id (which has to be in BLOCKED state) back to the READY state. If the resumed thread has a higher priority than the running thread a context switch occurs immediately.

The thread becomes ready regardless of the reason why the thread was blocked. Thus it is not recommended to resume a thread not suspended by osThreadSuspend.

Functions that will put a thread into BLOCKED state are: osEventFlagsWait and osThreadFlagsWait, osDelay and osDelayUntil, osMutexAcquire and osSemaphoreAcquire, osMessageQueueGet, osMemoryPoolAlloc, osThreadJoin, osThreadSuspend.

Possible osStatus_t return values:

  • osOK: the thread has been resumed successfully.
  • osErrorParameter: thread_id is NULL or invalid.
  • osErrorResource: the thread is in an invalid state.
  • osErrorISR: the function osThreadResume cannot be called from interrupt service routines.
Note
This function cannot be called from Interrupt Service Routines.
This function may be called when kernel is locked (osKernelLock). Under this circumstances a potential context switch is delayed until the kernel gets unlocked, i.e. osKernelUnlock or osKernelRestoreLock.
osStatus_t osThreadDetach ( osThreadId_t  thread_id)
Parameters
[in]thread_idthread ID obtained by osThreadNew or osThreadGetId.
Returns
status code that indicates the execution status of the function.

The function osThreadDetach changes the attribute of a thread (specified by thread_id) to osThreadDetached. Detached threads are not joinable with osThreadJoin. When a detached thread is terminated, all resources are returned to the system. The behavior of osThreadDetach on an already detached thread is undefined.

Possible osStatus_t return values:

  • osOK: the attribute of the specified thread has been changed to detached successfully.
  • osErrorParameter: thread_id is NULL or invalid.
  • osErrorResource: the thread is in an invalid state.
  • osErrorISR: the function osThreadDetach cannot be called from interrupt service routines.
Note
This function cannot be called from Interrupt Service Routines.
osStatus_t osThreadJoin ( osThreadId_t  thread_id)
Parameters
[in]thread_idthread ID obtained by osThreadNew or osThreadGetId.
Returns
status code that indicates the execution status of the function.

The function osThreadJoin waits for the thread specified by thread_id to terminate. If that thread has already terminated, then osThreadJoin returns immediately. The thread must be joinable. By default threads are created with the attribute osThreadDetached.

Possible osStatus_t return values:

  • osOK: if the thread has already been terminated and joined or once the thread has been terminated and the join operations succeeds.
  • osErrorParameter: thread_id is NULL or invalid.
  • osErrorResource: the thread is in an invalid state (ex: not joinable).
  • osErrorISR: the function osThreadJoin cannot be called from interrupt service routines.
Note
This function cannot be called from Interrupt Service Routines.
Only one thread shall call osThreadJoin to join the target thread. If multiple threads try to join simultaneously with the same thread, the results are undefined.
__NO_RETURN void osThreadExit ( void  )

The function osThreadExit terminates the calling thread. This allows the thread to be synchronized with osThreadJoin.

Note
This function cannot be called from Interrupt Service Routines.

Code Example

__NO_RETURN void worker (void *argument) {
// do something
osDelay(1000U);
}
osStatus_t osThreadTerminate ( osThreadId_t  thread_id)
Parameters
[in]thread_idthread ID obtained by osThreadNew or osThreadGetId.
Returns
status code that indicates the execution status of the function.

The function osThreadTerminate removes the thread specified by parameter thread_id from the list of active threads. If the thread is currently RUNNING, the thread terminates and the execution continues with the next READY thread. If no such thread exists, the function will not terminate the running thread, but return osErrorResource.

Possible osStatus_t return values:

  • osOK: the specified thread has been removed from the active thread list successfully.
  • osErrorParameter: thread_id is NULL or invalid.
  • osErrorResource: the thread is in an invalid state or no other READY thread exists.
  • osErrorISR: the function osThreadTerminate cannot be called from interrupt service routines.
Note
This function cannot be called from Interrupt Service Routines.
Avoid calling the function with a thread_id that does not exist or has been terminated already.
osThreadTerminate destroys non-joinable threads and removes their thread_id from the system. Subsequent access to the thread_id (for example osThreadGetState) will return an osThreadError. Joinable threads will not be destroyed and return the status osThreadTerminated until they are joined with osThreadJoin.

Code Example

#include "cmsis_os2.h"
void Thread_1 (void *arg); // function prototype for Thread_1
void ThreadTerminate_example (void) {
osStatus_t status;
id = osThreadNew(Thread_1, NULL, NULL); // create the thread
// do something
status = osThreadTerminate(id); // stop the thread
if (status == osOK) {
// Thread was terminated successfully
}
else {
// Failed to terminate a thread
}
}
uint32_t osThreadGetStackSize ( osThreadId_t  thread_id)
Parameters
[in]thread_idthread ID obtained by osThreadNew or osThreadGetId.
Returns
stack size in bytes.

The function osThreadGetStackSize returns the stack size of the thread specified by parameter thread_id. In case of an error, it returns 0.

Note
This function cannot be called from Interrupt Service Routines.
uint32_t osThreadGetStackSpace ( osThreadId_t  thread_id)
Parameters
[in]thread_idthread ID obtained by osThreadNew or osThreadGetId.
Returns
remaining stack space in bytes.

The function osThreadGetStackSpace returns the size of unused stack space for the thread specified by parameter thread_id. Stack watermark recording during execution needs to be enabled (refer to Thread Configuration). In case of an error, it returns 0.

Note
This function cannot be called from Interrupt Service Routines.
uint32_t osThreadGetCount ( void  )
Returns
number of active threads.

The function osThreadGetCount returns the number of active threads or 0 in case of an error.

Note
This function cannot be called from Interrupt Service Routines.
uint32_t osThreadEnumerate ( osThreadId_t thread_array,
uint32_t  array_items 
)
Parameters
[out]thread_arraypointer to array for retrieving thread IDs.
[in]array_itemsmaximum number of items in array for retrieving thread IDs.
Returns
number of enumerated threads.

The function osThreadEnumerate returns the number of enumerated threads or 0 in case of an error.

Note
This function cannot be called from Interrupt Service Routines.