Skip to content
Permalink
master
Switch branches/tags

Name already in use

A tag already exists with the provided branch name. Many Git commands accept both tag and branch names, so creating this branch may cause unexpected behavior. Are you sure you want to create this branch?
Go to file
 
 
Cannot retrieve contributors at this time

Thread Priorities

In this task we will examine setting thread priority's using MBED

This task will have to be performed using the Hardware, as the simulator has no support for threading.

Example Code

#include <mbed.h>

//Mostly Global Functions
DigitalOut led1(LED1);
DigitalOut led2(LED2);
DigitalOut led3(LED3);

Thread threadOne;
Thread threadTwo;
Thread threadThree;

void led1Thread(){
  while(1){
    printf("LED 1\n");
    led1 = !led1;
    wait(1);
  }
}

void led2Thread(){
  while(1){
    printf("LED 2\n");
    led2 = !led2;
    wait(1);
  }
}

void led3Thread(){
  while(1){
    printf("LED 3");
    led3 = !led3;
    wait(1);
  }
}

int main() {

  // put your setup code here, to run once:
  threadOne.start(led1Thread);
  threadTwo.start(led2Thread);
  threadThree.start(led3Thread);

  //PAuse Main Loop
  wait_ms(osWaitForever);

}

We can Modify the Thread priority using

threadOne.set_priority(osPriorityLow);

TASK:

Modify the Proprieties of the Threads

  • For example, set Thread1 to Low Priority, and Thread Three to High Priority
  • What difference does this have on the operation
  • What is happening at a scheduler Level?

Thread Priorities and Blocking Tasks

In this final Example we are going to create a "Blocking" task. This task has approximately the same "Tick" as the scheduler so modifying the priority will change how the code executes.

void blockingThread(){
  while(1){
    led1 = !led1;
    for (int x=0; x<10000; x++){
      //Delay of Approx One Second
      wait(0.0001);
    }
  }

TASK

  • Replace one of the Threads in the Code above with the Blocking handler.

  • Change the property of this thread.

    • What happens when it is high priory
    • What happens when it is low priority/
  • Think About (and draw) what is happening with the Scheduler.