8 years, 8 months ago.

Thread switching on mbed RTOS, How can I control it?

Hi everyone,

I've been playing arround with the NUCLEO board and mbedRTOS. I've written this symple test;

Blink

#include "mbed.h"
#include "rtos.h"

DigitalOut led1(LED1);
int i = 0;

void ledOn(int times, float speed)
{
    for(i=0; i=times; i++) {
        led1 = 1;
        wait(speed);
        led1 = 0; 
        wait(speed*2); 
    }
}
void led1_thread(void const *args)
{
    while (true) {
        ledOn(3, 0.2);       
    }

}
void led1_blink_thread(void const *args)
{
    while (true) {
        ledOn(4, 6.0);    
    }

}
int main()
{
    Thread thread1(led1_thread);
    Thread thread2(led1_blink_thread);
    while (true) {
        Thread::wait(1000);
       
    }
}

Now I want to synch the threads, call thread 1 and block the thread2 until LedOn blinks 4 times. I've used signal synch. and mutex but it doesn't work. Any clues?

First of all: You want to blink with Thread::wait, instead of regular wait. Second, can you show code of when it goes wrong? In general I don't think mutexes are what you want for this, but more something like Signals.

posted by Erik - 08 Sep 2015

Ok, I see. I'm going to change the wait. I was thinking of Signals to synch the calls to the threads. What happens with the code that I've posted is that the scheduler is calling the threads and the LED blink frequency is a mix of the two, as once the LED resource is freed from Thread 1, the scheduler calls thread 2. Which make sense, as the idea is to have a pseudo parallel execution of threads. What I want is the thread 2 to wait until thread 1 has done a task, block thread 2 untill thread 1 has completed 4 blinks.

posted by Jose Hermoso 10 Sep 2015
Be the first to answer this question.