Creating and disposing threads stops working after ~60 cycles in 0.5.0-rc1

Hardware: Particle Photon

I am looking at the possibility of spawning off threads to carry out work, but also being able to terminate them early if I no longer need what they are doing. So I have written the following test program which spawns a thread to rapidly flash d7 LED, then disposes it and slowly flashes D7 and repeats. After ~60secs of running the rapid flashing stops indicating that no new threads are being created, then a few seconds later the RGB LED goes to breathing green (CLOUD NOT CONNECTED mode).

Perhaps some resource is not being cleaned up?

int LED = D7;
Thread hardwareThread;
int controlParams[1];

os_thread_return_t testMethod(void* params) {
    int *args = (int*) params;
    int interval = args[0];
    for(;;) {
        digitalWrite(LED, HIGH);
        delay(interval);
        digitalWrite(LED, LOW);
        delay(interval);
    }
}

void setup() {
    pinMode(LED, OUTPUT);
}

void loop() {
    controlParams[0] = 50;
    hardwareThread = Thread("hardware", testMethod, &controlParams);
    
    delay(1000);
    
    hardwareThread.dispose();

    digitalWrite(LED, HIGH);
    delay(1000);
    digitalWrite(LED, LOW);
    delay(1000);
}

thanks,
Chris

I would suggest rather than spawning threads and killing them when no longer needed that you instead just use a thread with a worker queue. When there is nothing in the queue, the thread will block - it will use very little cpu resources and just than the memory for it’s stack.

In freeRTOS tasks functions aren’t allowed to return so I’m under the impression that tasks are meant to be long lived.

I agree that using a worker queue seems like the best way, all the reading I have done in the freeRTOS forums and doc suggest that its going to be pretty hard to guarantee to cleanup all resources, especially if you terminate the thread, since there is no hook for the code to do that.

Since the original post I added publication of free memory to my test code and it clearly showed the memory linearly falling to zero.