4 years, 6 months ago.

MbedOS 5.0 thread termination

Hello All, In my understanding a good practice to terminate a thread is to ask it for a self-termination rather than killing it asynchronously from somewhere in the program. The “suicide” pattern allows the thread to release all its outstanding resources and properly stop/deinit the HW is managing if any. This is the compelling reason to have a “terminate()” function (from the Thread Class) working properly also when called from within the thread context. Unfortunately, from my tests, this seems not the case as the “terminate()” it works if called from outside the thread to be killed, while it hangs the system if it is called from within the thread to be killed, which is the right place in my humble opinion. Any suggestions? Thank you in advance, best regards

Question relating to:

Firmware Development for X-NUCLEO Expansion Boards

1 Answer

4 years, 6 months ago.

Hello Licio,

Having a thread handle its own exit is good practice. This can be done either by returning from the thread's main function or by calling terminate. If a thread is unable to terminate itself then this is a regression.

Can you provide any details on how the system hangs? Is the main thread trying to join the thread which terminates itself? A reproducible test case that I could run would be ideal.

Thanks, Russ

Accepted Answer

I can confirm that if a thread terminates itself, any other threads trying to join it will hang forever. I created this PR to both fix it and add a test to catch future regressions: https://github.com/ARMmbed/mbed-os/pull/2643

posted by Russ Butler 07 Sep 2016