8 years, 10 months ago.

how to reactivate/recreate a thread after finishing or termination?

I have the same problem like Carlos. I want to start executing a function in a thread and also be able to restart (stop and start it again) the same function executing also in a thread, doing it e.g for 50 times.

Actually i create and strat my thread by new-operator (pointer) and after executing or after using the terminate-method i kill the stuff with delete. The problem is i am able to do this just for 11 times (although the maximum number of useable threads is set to 7 in rtos library). After 11th execution i cant create a new thread.

So, what would you suggest to solved my software dilemma?

Regards.

Andreas

Question relating to:

1 Answer

8 years, 9 months ago.

Task Example

  1. include "mbed.h"
  2. include "rtos.h"

Serial pc(USBTX, USBRX); DigitalOut led1(LED1); DigitalOut led2(LED2); Thread *thp;

void led2_thread(void const *args) { pc.printf("led2_thread started %X\r\n", thp); led2 = !led2; thp->wait(2000); thp->terminate(); }

int main() { int state;

thp = new Thread(led2_thread);

pc.printf("Thread started thp = %X\r\n", thp);

while (true) { led1 = !led1; Thread::wait(500); state = thp->get_state(); pc.printf("Thread state %d\r\n", state); if(state == Thread::Inactive) { delete thp; thp = new Thread(led2_thread); pc.printf("Thread restarted\r\n"); } } end while }

Start/Stop/Restart a Thread seems to work in my example. I run it for about an hour without any problem. My initial problem was, that I deleted the Thread object within the object itself. As a result the creation of the Thread failed after the 11’ time. Cheers, Guenter.