Mistake on this page?
Report an issue in GitHub or email us

Thread

The Thread class allows defining, creating and controlling parallel tasks.

Note: The function main is a special thread function that is started at system initialization.

Thread class reference

Public Types
enum  State {
  Inactive, Ready, Running, WaitingDelay,
  WaitingJoin, WaitingThreadFlag, WaitingEventFlag, WaitingMutex,
  WaitingSemaphore, WaitingMemoryPool, WaitingMessageGet, WaitingMessagePut,
  WaitingInterval, WaitingOr, WaitingAnd, WaitingMailbox,
  Deleted
}
Public Member Functions
 Thread (osPriority priority=osPriorityNormal, uint32_t stack_size=OS_STACK_SIZE, unsigned char *stack_mem=NULL, const char *name=NULL)
 Thread (mbed::Callback< void()> task, osPriority priority=osPriorityNormal, uint32_t stack_size=OS_STACK_SIZE, unsigned char *stack_mem=NULL)
template<typename T >
 Thread (T *argument, void(T::*task)(), osPriority priority=osPriorityNormal, uint32_t stack_size=OS_STACK_SIZE, unsigned char *stack_mem=NULL)
template<typename T >
 Thread (T *argument, void(*task)(T *), osPriority priority=osPriorityNormal, uint32_t stack_size=OS_STACK_SIZE, unsigned char *stack_mem=NULL)
 Thread (void(*task)(void const *argument), void *argument=NULL, osPriority priority=osPriorityNormal, uint32_t stack_size=OS_STACK_SIZE, unsigned char *stack_mem=NULL)
osStatus start (mbed::Callback< void()> task)
template<typename T , typename M >
osStatus start (T *obj, M method)
osStatus join ()
osStatus terminate ()
osStatus set_priority (osPriority priority)
osPriority get_priority ()
int32_t signal_set (int32_t signals)
State get_state ()
uint32_t stack_size ()
uint32_t free_stack ()
uint32_t used_stack ()
uint32_t max_stack ()
const char * get_name ()
Static Public Member Functions
static int32_t signal_clr (int32_t signals)
static osEvent signal_wait (int32_t signals, uint32_t millisec=osWaitForever)
static osStatus wait (uint32_t millisec)
static osStatus yield ()
static osThreadId gettid ()
static void attach_idle_hook (void(*fptr)(void))
static void attach_terminate_hook (void(*fptr)(osThreadId id))

Thread example

The code below uses two separate threads to blink two LEDs. The first thread is automatically created and executes the main function; the second thread is created explicitly inside main.

#include "mbed.h"
 
DigitalOut led1(LED1);
DigitalOut led2(LED2);
Thread thread;
 
void led2_thread() {
    while (true) {
        led2 = !led2;
        wait(1);
    }
}
 
int main() {
    thread.start(led2_thread);
    
    while (true) {
        led1 = !led1;
        wait(0.5);
    }
}

Thread example with callbacks

The Callback API provides a convenient way to pass arguments to spawned threads.

#include "mbed.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();
}
Important Information for this Arm website

This site uses cookies to store information on your computer. By continuing to use our site, you consent to our cookies. If you are not happy with the use of these cookies, please review our Cookie Policy to learn how they can be disabled. By disabling cookies, some features of the site will not work.