ChibiOS/RT Architecture - Reference Manual - Guides |
In ChibiOS/RT threads are divided in two categories:
Dynamic threads create the problem of who is responsible of releasing their memory because a thread cannot dispose its own memory.
This is handled in ChibiOS/RT through the mechanism of "thread references", When the CH_USE_DYNAMIC
option is enabled the threads become objects with a reference counter. The memory of a thread, if dynamic, is freed when the last reference to the thread is released while the thread is in its THD_STATE_FINAL
state.
The following diagram explains the mechanism:
As you can see the easiest way to ensure that the memory is released is to make another thread perform a chThdWait()
on the dynamic thread.
If all the references to the threads are released while the thread is still alive then the thread goes in a "detached" state and its memory cannot be recovered unless there is a dedicated task in the system that scans the threads through the Registry subsystem, scanning the registry has the side effect to release the zombies (detached and then terminated threads).