<li><b>Static threads</b>. The memory used for static threads is allocated at compile time so static threads are always there, there is no management to be done.</li>
<li><b>Dynamic threads</b>. Dynamic threads are allocated at runtime from one of the available allocators (see <aclass="el"href="group__heaps.html">Heaps</a>, <aclass="el"href="group__pools.html">Memory Pools</a>).</li>
</ul>
<p>Dynamic threads create the problem of who is responsible of releasing their memory because a thread cannot dispose its own memory.<br/>
This is handled in ChibiOS/RT through the mechanism of "thread references", When the <code>CH_USE_DYNAMIC</code> 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 <code>THD_STATE_FINAL</code> state.<br/>
The following diagram explains the mechanism: </p>
As you can see the easiest way to ensure that the memory is released is to make another thread perform a <code><aclass="el"href="group__threads.html#ga94a5727ecdbe0f738dfc8e1ede1c3409"title="Blocks the execution of the invoking thread until the specified thread terminates then the exit code ...">chThdWait()</a></code> on the dynamic thread.<br/>
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 <aclass="el"href="group__registry.html">Registry</a> subsystem, scanning the registry has the side effect to release the zombies (detached and then terminated threads). </p>
Generated on Sun Nov 28 2010 14:09:55 for ChibiOS/RT by <ahref="http://www.doxygen.org/index.html"><imgsrc="doxygen.png"alt="doxygen"align="middle"border="0"></a> 1.7.1</small></address>