<li><b>Idle thread</b>. This thread has the lowest priority in the system so it runs only when the other threads in the system are sleeping. This threads usually switches the system in a low power mode and does nothing else.</li>
<li><b>Main thread</b>. This thread executes your <code>main()</code> function at startup. The main thread is created at the <code>NORMALPRIO</code> level but it can change its own priority if required. It is from the main thread that the other threads are usually created.</li>
</ul>
<p>There are two kind of threads in ChibiOS/RT:</p>
<ul>
<li><b>Static Threads</b>. This kind of threads are statically allocated in memory. The memory used by the thread cannot reused except for restarting the threads.</li>
<li><b>Dynamic Threads</b>. Threads created by allocating memory from a memory heap or a memory pool.</li>
</ul>
<h2>Creating a static thread</h2>
<p>In order to create a static thread a working area must be declared using the macro <code>WORKING_AREA</code> as shown: </p>
<divclass="fragment"><preclass="fragment"><spanclass="keyword">static</span><aclass="code"href="group__core.html#gac8b681d521d3b6c25e7a0304674732c9"title="Static working area allocation.">WORKING_AREA</a>(myThreadWorkingArea, 128);
</pre></div><p> This macro reserves 128 bytes of stack for the thread and space for all the required thread related structures. The total size and the alignment problems are handled inside the macro, you only need to specify the pure stack size.<br/>
A thread can be started by invoking <code><aclass="el"href="group__threads.html#ga048e588238dd336c6059133b3d0c3435"title="Creates a new thread into a static memory area.">chThdCreateStatic()</a></code> as shown in this example: </p>
<divclass="fragment"><preclass="fragment"><aclass="code"href="struct_thread.html"title="Structure representing a thread.">Thread</a> *tp = <aclass="code"href="group__threads.html#ga048e588238dd336c6059133b3d0c3435"title="Creates a new thread into a static memory area.">chThdCreateStatic</a>(myThreadWorkingArea,
<spanclass="comment"> * My simple application.</span>
<spanclass="comment"> */</span>
<spanclass="preprocessor">#include <<aclass="code"href="ch_8h.html"title="ChibiOS/RT main include file.">ch.h</a>></span>
<spanclass="comment">/*</span>
<spanclass="comment"> * Working area for the LED flashing thread.</span>
<spanclass="comment"> */</span>
<spanclass="keyword">static</span><aclass="code"href="group__core.html#gac8b681d521d3b6c25e7a0304674732c9"title="Static working area allocation.">WORKING_AREA</a>(myThreadWorkingArea, 128);
<spanclass="comment">/*</span>
<spanclass="comment"> * LED flashing thread.</span>
<spanclass="comment"> */</span>
<spanclass="keyword">static</span><aclass="code"href="group__types.html#ga35bcb0c321cd7bc45bf1a11fa17ebdd3"title="Message, use signed pointer equivalent.">msg_t</a> myThread(<spanclass="keywordtype">void</span> *arg) {
<spanclass="keywordflow">while</span> (TRUE) {
LED_ON();
<aclass="code"href="group__threads.html#ga5aebc25e053a0094714abce4cad9f56f"title="Delays the invoking thread for the specified number of milliseconds.">chThdSleepMilliseconds</a>(500);
LED_OFF();
<aclass="code"href="group__threads.html#ga5aebc25e053a0094714abce4cad9f56f"title="Delays the invoking thread for the specified number of milliseconds.">chThdSleepMilliseconds</a>(500);
<spanclass="comment">/* Starting the flashing LEDs thread.*/</span>
(void)<aclass="code"href="group__threads.html#ga048e588238dd336c6059133b3d0c3435"title="Creates a new thread into a static memory area.">chThdCreateStatic</a>(myThreadWorkingArea, <spanclass="keyword">sizeof</span>(myThreadWorkingArea),
</pre></div><p> Note that the memory allocated to myThread() is statically defined and cannot be reused. Static threads are ideal for safety applications because there is no risk of a memory allocation failure because progressive heap fragmentation.</p>
<h2>Creating a dynamic thread using the heap allocator</h2>
<p>In order to create a thread from a memory heap is very easy: </p>
<divclass="fragment"><preclass="fragment"><aclass="code"href="struct_thread.html"title="Structure representing a thread.">Thread</a> *tp = <aclass="code"href="group__threads.html#ga50b84e3e82a4e09c1066e1d422e4c780"title="Creates a new thread allocating the memory from the heap.">chThdCreateFromHeap</a>(NULL, <spanclass="comment">/* NULL = Default heap. */</span>
<aclass="code"href="group__core.html#gaf7a83c1c8bde96b77299c36dc598d33d"title="Computes the thread working area global size.">THD_WA_SIZE</a>(128),<spanclass="comment">/* Stack size. */</span>
<aclass="code"href="group__scheduler.html#gad6a6b28f0c1cf8e0da714ef771fb90a0"title="Normal user priority.">NORMALPRIO</a>, <spanclass="comment">/* Initial priority. */</span>
</pre></div><p> The memory is allocated from the spawned heap and the thread is started. Note that the memory is not freed when the thread terminates but when the thread final status (its return value) is collected by the spawning thread. As example: </p>
<divclass="fragment"><preclass="fragment"><spanclass="keyword">static</span><aclass="code"href="group__types.html#ga35bcb0c321cd7bc45bf1a11fa17ebdd3"title="Message, use signed pointer equivalent.">msg_t</a> myThread(<spanclass="keywordtype">void</span> *arg) {
<spanclass="keywordtype">unsigned</span> i = 10;
<spanclass="keywordflow">while</span> (i > 0) {
LED_ON();
<aclass="code"href="group__threads.html#ga5aebc25e053a0094714abce4cad9f56f"title="Delays the invoking thread for the specified number of milliseconds.">chThdSleepMilliseconds</a>(500);
LED_OFF();
<aclass="code"href="group__threads.html#ga5aebc25e053a0094714abce4cad9f56f"title="Delays the invoking thread for the specified number of milliseconds.">chThdSleepMilliseconds</a>(500);
i--;
}
<spanclass="keywordflow">return</span> (<aclass="code"href="group__types.html#ga35bcb0c321cd7bc45bf1a11fa17ebdd3"title="Message, use signed pointer equivalent.">msg_t</a>)i;
<aclass="code"href="struct_thread.html"title="Structure representing a thread.">Thread</a> *tp = <aclass="code"href="group__threads.html#ga50b84e3e82a4e09c1066e1d422e4c780"title="Creates a new thread allocating the memory from the heap.">chThdCreateFromHeap</a>(NULL, <aclass="code"href="group__core.html#gaf7a83c1c8bde96b77299c36dc598d33d"title="Computes the thread working area global size.">THD_WA_SIZE</a>(128), <aclass="code"href="group__scheduler.html#gad6a6b28f0c1cf8e0da714ef771fb90a0"title="Normal user priority.">NORMALPRIO</a>+1,
myThread, NULL);
<spanclass="keywordflow">if</span> (tp == NULL)
<aclass="code"href="group__system.html#gad43b78f160a2c983792af3041cc4a536"title="Halts the system.">chSysHalt</a>(); <spanclass="comment">/* Memory exausted. */</span>
<spanclass="comment">/* The main thread continues its normal execution.*/</span>
.
.
<spanclass="comment">/*</span>
<spanclass="comment"> * Now waits for the spawned thread to terminate (if it has not terminated</span>
<spanclass="comment"> * already) then gets the thread exit message (msg) and returns the</span>
<spanclass="comment"> * terminated thread memory to the heap (default system heap in this</span>
<aclass="code"href="group__types.html#ga35bcb0c321cd7bc45bf1a11fa17ebdd3"title="Message, use signed pointer equivalent.">msg_t</a> msg = <aclass="code"href="group__threads.html#ga94a5727ecdbe0f738dfc8e1ede1c3409"title="Blocks the execution of the invoking thread until the specified thread terminates then the exit code ...">chThdWait</a>(tp);
</pre></div><h2>Creating a dynamic thread using the heap allocator</h2>
<p>A pool is a collection of equally sized memory blocks, creating a thread from a memry pool is very similar to the previous example but the memory of terminated threads is returned to the memory pool rather than to a heap: </p>
<divclass="fragment"><preclass="fragment"><spanclass="keyword">static</span><aclass="code"href="group__types.html#ga35bcb0c321cd7bc45bf1a11fa17ebdd3"title="Message, use signed pointer equivalent.">msg_t</a> myThread(<spanclass="keywordtype">void</span> *arg) {
<spanclass="keywordtype">unsigned</span> i = 10;
<spanclass="keywordflow">while</span> (i > 0) {
LED_ON();
<aclass="code"href="group__threads.html#ga5aebc25e053a0094714abce4cad9f56f"title="Delays the invoking thread for the specified number of milliseconds.">chThdSleepMilliseconds</a>(500);
LED_OFF();
<aclass="code"href="group__threads.html#ga5aebc25e053a0094714abce4cad9f56f"title="Delays the invoking thread for the specified number of milliseconds.">chThdSleepMilliseconds</a>(500);
i--;
}
<spanclass="keywordflow">return</span> (<aclass="code"href="group__types.html#ga35bcb0c321cd7bc45bf1a11fa17ebdd3"title="Message, use signed pointer equivalent.">msg_t</a>)i;
<aclass="code"href="struct_thread.html"title="Structure representing a thread.">Thread</a> *tp = <aclass="code"href="group__threads.html#gad3ccc1a6f054c543258ed2fe97ed2f58"title="Creates a new thread allocating the memory from the specified memory pool.">chThdCreateFromMemoryPool</a>(myPool, <aclass="code"href="group__scheduler.html#gad6a6b28f0c1cf8e0da714ef771fb90a0"title="Normal user priority.">NORMALPRIO</a>+1, myThread, NULL);
<aclass="code"href="group__system.html#gad43b78f160a2c983792af3041cc4a536"title="Halts the system.">chSysHalt</a>(); <spanclass="comment">/* Pool empty. */</span>
<spanclass="comment">/* The main thread continues its normal execution.*/</span>
.
.
<spanclass="comment">/*</span>
<spanclass="comment"> * Now waits for the spawned thread to terminate (if it has not terminated</span>
<spanclass="comment"> * already) then gets the thread exit message (msg) and returns the</span>
<spanclass="comment"> * terminated thread memory to the original memory pool.</span>
<aclass="code"href="group__types.html#ga35bcb0c321cd7bc45bf1a11fa17ebdd3"title="Message, use signed pointer equivalent.">msg_t</a> msg = <aclass="code"href="group__threads.html#ga94a5727ecdbe0f738dfc8e1ede1c3409"title="Blocks the execution of the invoking thread until the specified thread terminates then the exit code ...">chThdWait</a>(tp);
Generated on Sun Nov 28 2010 14:09:54 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>