7 years, 2 months ago.

Thread.start(func) error -4

I have started a thread using mbed 5.5.6 and let it run to completion. ex: Thread myThread; osStatus stat=myThread.start(func);

by printing stat it equals 0 - thread started correctly.

func runs to completion. I then restart it

osStatus stat=myThread.start(func);

the thread never restarts and stat=-4, -4 is not defined as an error code in cmsis_os.h so I have no idea what has happened.

Please post your full code in "<<code>>" block.

posted by Mark Peter Vargha 10 Sep 2017

The code is too large to post here, I have a work around now but if you are interested in solving the issue I suggest you try to create a small code snippit and try it.

posted by Dan Sexton 11 Sep 2017

try this:

#include "mbed.h"

Thread myThread;

Serial db(PTB17, PTB16);

bool flag=false;

void func()
{
    flag=true;
}


int main()
{
    db.baud(115200);

    int iret=myThread.start(func);

    db.printf("thread started code %i\r\n",iret);

    while(flag==false){Thread::wait(10);}

    db.printf("Thread completed\r\n");

    iret=myThread.start(func);

    db.printf("thread error code %i\r\n",iret);

}
posted by Dan Sexton 11 Sep 2017

1 Answer

7 years, 2 months ago.

At the moment, restarting a completed thread this way is unsupported. Threads are required in embedded systems to be both lightweight and simple. Your best bet is to create a new thread or use placement new

Your answer is surprising as object creation is usually not a lightweight operation. In addition this method worked in previous versions of MBED OS. It would seem that restarting a thread this way is no heavier than starting it in the first place. IMHO this is a bug. I have worked around this by putting the thread in a while(true) loop with a wait on signal at the end to restart it. Although less than satisfying it does work.

posted by Dan Sexton 21 Sep 2017

You're right, it is a known bug and there has been discussion in adding it to future releases.

posted by Michael Bartling 21 Sep 2017