Lee Shen / FTHR_USB_serial_qSPI
Embed: (wiki syntax)

« Back to documentation index

Thread Class Reference

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.

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 72 of file Thread.h.


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:
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: NULL).
namename to be used for this thread. It has to stay allocated for the lifetime of the thread (default: NULL)

Definition at line 80 of file Thread.h.