C语言封装的轻量线程环境 Protothreads

fmms 13年前
     <p>Protothreads是一种针对C语言封装后的宏函数库,为C语言模拟了一种无堆栈的轻量线程环境,能够实现模拟线程的条件阻塞、信号量操作等操作<span class="UBBWordLink">系统</span>中特有的机制,从而使程序实现多线程操作。每个Protothreads线程仅增加10行代码和2字节RAM的额外硬件资源消耗。对于资源紧缺而不能移植嵌入式操作<span class="UBBWordLink">系统</span>的嵌入式<span class="UBBWordLink">系统</span>,使用Protothreads能够方便直观地<span class="UBBWordLink">设计</span>多任务程序,能够实现用线性程序结构处理事件驱动型程序和状态机程序,简化了该类程序的<span class="UBBWordLink">设计。</span></p>    <p>主要特性:</p>    <ul>     <li>Very small RAM overhead - only two bytes per protothread and no extra stacks</li>     <li>Highly portable - the protothreads library is 100% pure C and no architecture specific assembly code</li>     <li>Can be used with or without an OS</li>     <li>Provides blocking wait without full multi-threading or stack-switching</li>     <li>Freely available under a BSD-like open source license</li>    </ul>    <p>示例程序:</p>    <ul>     <li>Memory constrained systems</li>     <li>Event-driven protocol stacks</li>     <li>Small embedded systems</li>     <li>Sensor network nodes</li>     <li>Portable C applications</li>    </ul>    <p><span class="UBBWordLink">示例代码:</span></p>    <pre class="brush:cpp; toolbar: true; auto-links: false;">#include "pt.h"   struct pt pt; struct timer timer;   PT_THREAD(example(struct pt *pt)) {   PT_BEGIN(pt);     while(1) {     if(initiate_io()) {       timer_start(&timer);       PT_WAIT_UNTIL(pt,          io_completed() ||          timer_expired(&timer));       read_data();     }   }   PT_END(pt); }</pre>    <p><strong>项目主页:</strong><a href="http://www.open-open.com/lib/view/home/1326937925140" target="_blank">http://www.open-open.com/lib/view/home/1326937925140</a></p>    <p></p>