gnuk/ChibiOS_2.0.8/docs/html/article_lifecycle.html
2010-11-30 13:54:43 +09:00

61 lines
3.7 KiB
HTML

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html><head><meta http-equiv="Content-Type" content="text/html;charset=UTF-8">
<title>ChibiOS/RT: Threads Lifecycle</title>
<link href="custom.css" rel="stylesheet" type="text/css">
<link href="tabs.css" rel="stylesheet" type="text/css">
</head><body>
<table style="text-align: center; width: 100%;" border="0"
cellpadding="2" cellspacing="2">
<tbody>
<tr>
<td style="width: 80px;"><img alt="ChibiOS/RT Logo" src="logo_small.png"></td>
<td><big><big>ChibiOS/RT</big></big><br><br>Architecture - Reference Manual - Guides</td>
<td style="width: 80px;"></td>
</tr>
</tbody>
</table>
<hr size="1">
<!-- Generated by Doxygen 1.7.1 -->
<div class="navigation" id="top">
<div class="tabs">
<ul class="tablist">
<li><a href="main.html"><span>Main&nbsp;Page</span></a></li>
<li><a href="modules.html"><span>Modules</span></a></li>
<li><a href="annotated.html"><span>Data&nbsp;Structures</span></a></li>
<li><a href="files.html"><span>Files</span></a></li>
</ul>
</div>
<div class="navpath">
<ul>
<li><a class="el" href="main.html">ChibiOS/RT</a> </li>
<li><a class="el" href="articles.html">Articles and Code Samples</a> </li>
<li><a class="el" href="page_kb.html">Knowledge Base</a> </li>
</ul>
</div>
</div>
<div class="header">
<div class="headertitle">
<h1>Threads Lifecycle </h1> </div>
</div>
<div class="contents">
<p>In ChibiOS/RT threads are divided in two categories:</p>
<ul>
<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 <a class="el" href="group__heaps.html">Heaps</a>, <a class="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>
<div align="center">
<img src="inline_dotgraph_14.dot.png" alt="inline_dotgraph_14.dot" border="0" usemap="#inline_dotgraph_14.dot.map">
<map name="inline_dotgraph_14.dot.map" id="inline_dotgraph_14.dot.map"></map>
</div>
<p> <br/>
As you can see the easiest way to ensure that the memory is released is to make another thread perform a <code><a class="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 <a class="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>
</div>
<hr size="1"><address style="text-align: right;"><small>
Generated on Sun Nov 28 2010 14:09:55 for ChibiOS/RT by&nbsp;<a href="http://www.doxygen.org/index.html"><img src="doxygen.png" alt="doxygen" align="middle" border="0"></a> 1.7.1</small></address>
</body>
</html>