9 years ago.

Problem with thread activation in mbed-rtos (K64F platform)

I am trying to use mbed-rtos with a K64F platform. If I use the frdm-rtos example on the K64F platform page, it works fine.

If I change the program slightly to put the led thread in a different file, and create that thread by a call from main(), the led thread apparently doesn't run. The led does not blink. The button thread continues to work as before.

I apparently don't understand something about thread creation. Any ideas why my change doesn't work?

Thanks.

Here are the modified files:

modified main.cpp:

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

// move this declaration into led.cpp
//DigitalOut led1(LED1); 
 
InterruptIn sw2(SW2);
uint32_t button_pressed;
Thread *thread2;

void sw2_press(void)
{
    thread2->signal_set(0x1);
}

// declare the function in led.cpp that creates the thread
extern void create_led_thread(void);

// move the led thread code into led.cpp
/*
void led_thread(void const *argument)
{
    while (true) {
        led1 = !led1;
        Thread::wait(1000);
    }
}
*/

void button_thread(void const *argument)
{
    while (true) {
        Thread::signal_wait(0x1);
        button_pressed++;
    }
}

int main()
{
// don't create the thread here; instead call create_led_thread() to create it
//    Thread thread(led_thread);
    create_led_thread();
    
    thread2 = new Thread(button_thread);

    button_pressed = 0;
    sw2.fall(&sw2_press);
    while (true) {
        Thread::wait(5000);
        printf("SW2 was pressed (last 5 seconds): %d \n", button_pressed);
        fflush(stdout);
        button_pressed = 0;
    }
}

new file led.cpp:

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

DigitalOut led1(LED1);

void led_thread(void const *argument)
{
    while (true) {
        led1 = !led1;
        Thread::wait(1000);
    }
}

void create_led_thread(void)
{
        Thread thread(led_thread);
        return;
}

2 Answers

9 years ago.

You make the Thread thread(led_thread) statically. As soon as the area it is made in is left, it is deleted again. Similar to if you would define there int randomValue = 5;, it would also be deallocated when that function is finished.

Either you would need to make it in main, which never finishes normally, or use 'new', as is also done in your main function. Then it is dynamically allocated and will stay around forever until explicitly deleted again.

Accepted Answer

Thanks Erik. I guess I thought of a thread as a different sort of creaturesomething inherently more global and persistent.

posted by Jim Weitenhagen 29 Apr 2015
-deleted-
9 years ago.

What happens when you create a led.h file and then in main.cpp add an include:

led.h

extern void led_thread(void const *argument);
extern void create_led_thread(void);

main.cpp

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

Thanks for the reply Ed.

Same behavior. Button thread works and led thread does not.

posted by Jim Weitenhagen 27 Apr 2015