6 years, 9 months ago.

Terminating and starting a thread?

Is it possible to terminate a thread and again start it from where it was terminated? My scenario

Thread send;
void send_message()
{
//1st do something
send.terminate();
//2nd do something
Thread::wait(20000);
}
void main(){
send.start(send_message);
}

So, I want to terminate the thread for some time after "1st do something" and when i start the thread back i want it to continue from "2nd do something". Can we do something like this? P.S. I will start the thread by setting some flag values.

1 Answer

6 years, 9 months ago.

Do not terminate thread. Just wait for a signal, mailbox, semaphore or event.

Example: osSignalWait (0x0001, osWaitForever); https://www.keil.com/pack/doc/CMSIS/RTOS/html/group__CMSIS__RTOS__SignalMgmt.html

Se full RTOS task management here: https://docs.mbed.com/docs/mbed-os-api-reference/en/latest/APIs/tasks/rtos/

Accepted Answer

Hi Mark,

Thanks for the reply, I will look into the links.

posted by Amod Amatya 09 Jun 2017

Hi Mark,

I was able to do it in this way. Thanks for your reply

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

Semaphore sem(0);

Serial pc(USBTX, USBRX); // tx, rx
Thread a;
void display()
{
    pc.printf("Hello");
    sem.wait();
    pc.printf("World");

}
int main() {
     pc.printf("Semaphores\n");
     a.start(display);
      char c = pc.getc();
          if(c == 'a')
              sem.release();
}
posted by Amod Amatya 11 Jun 2017