Arm Mbed OS support forum

Saying Hi, and looking for help in re-starting a thread

Saubona and Hello,
My first post here, so be gentle please. I am still feeling my way about ARM, Mbed and this environment, site and forum.
I have set up some blurb as an intro in my profile. I am trying to get a little up the ARm learning curve using MBed Studio 1.1.0, and I got a Nucleo-F446ZE to play with a few days ago.
I have some basic stuff working, blinky, ADC, buttons etc, but now I am experimenting with threads, but hit my head in trying to re-start a thread that ended.
The full code is appended, but the issue looks like it is here:-
printf (“status A: %d \n”, thread.get_state());

    if (thread.get_state() == Thread::Deleted)


        printf ("status B: %d \n", thread.get_state());

        thread.start(led2_thread); // kick off the thread again 


oops - I seem to post before I was ready to. As I said I am starting out here . . .

. . . to continue
I am trying to use if (thread.get_state() == Thread::Deleted) to see if the thread has ended, and it seems to work.
I then use
thread.start(led2_thread); // kick off the thread again to try to kick it off again but that seems to be the bit that does not work, It does not seem to restart.

I am wondering the best way to check if a thread has ‘died’ and then kick it off again.

Beginner question ?


Full code :-
#include “mbed.h”

// ============= global definitions ============

// create two LED objects

DigitalOut led1(LED1);

DigitalOut led2(LED2);

// create a thread

Thread thread;

// create a procedure that will be run as a thread

void led2_thread()


// loop 20 times and then die 

for (int i = 0; i < 21  i++)


    led2 = !led2; // toggle the LED  

    ThisThread::sleep_for(2500); // take a nap



// main entry point - will run as a thread

int main()


Timer t                        // create a timer to terminate 

t.start()                      // start the timer 

thread.start(led2_thread);      // kick off the thread first time

while (true) 


    led1 = !led1;               // toggle the LED 


    printf ("status A: %d \n", thread.get_state());

    if (thread.get_state() == Thread::Deleted)


        printf ("status B: %d \n", thread.get_state());

        thread.start(led2_thread); // kick off the thread again 



    if ( chrono::duration_cast<chrono::seconds>(t.elapsed_time()).count() > 100 ) // die with dignity


        printf ("status C: %d \n", thread.get_state());







Hello Peter,

  • already published post can be still edited :slight_smile:
  • for better reading of your code, please be so kind and use following method
   //your code

I am not professional but I think that is normal behavior. Once thread ends , it can’t be started again. You must create new one instead or make a rules inside the thread.

BR, Jan

Thanks for the help, my post was a bit of a mess - still coming to grips with this forum !!
Thanks for the posting tips - needed :grinning:
Okay - I will try to re-create a thread in main and see what happens.

Aah - so that’s how to edit.

But it seems, from some research, that threads are more complex that I thought. More research needed - but that’s why I am here

Creating and deleting threads is expensive, I wouldn’t do it. You can add a loop and wait for a semaphore. After the action inside the thread has finished, it will wait at the semaphore for the next trigger. This blocking will not consume cpu power.
Another way is to schedule functions as events in an EventQueue, this will work even in bare-metal. With the limit that you have only one thread to handle the queue events.

Thanks for this.
I did some research and what I found agrees with this.
I am still trying to come to grips with all these things to assist with threading like semaphores flags and queues. Looks like good stuff to know
In the meantime I experimented with just using volatile variables to do the same thing, but I am concerned that even the use of volatile global variables may not be thread safe.

But my next iteration looked as included.


Now to work out how to post code properly . . . .

What was meant by … and … ? I tried but it was a bit of a mess !!

And on the topic of being new to this forum, and ARM and Mbed, I have been compiling a few documents on the background to why I am trying to get up the ARM/Mbed learning curve, and my experiences so far.

My attempts at threading are discussed in Doc 09 - First steps in threading.

The docs were started to try to get a bit of interest in this environment at the Durban Makerspace, but there is not much going on there thanks to nasty Covid-19

I have just dumped the stuff here. Not sure if there is benefit or interest in this, and if there is what to do.