C語言封裝的輕量線程環境 Protothreads
Protothreads是一種針對C語言封裝后的宏函數庫,為C語言模擬了一種無堆棧的輕量線程環境,能夠實現模擬線程的條件阻塞、信號量操作等操作系統中特有的機制,從而使程序實現多線程操作。每個Protothreads線程僅增加10行代碼和2字節RAM的額外硬件資源消耗。對于資源緊缺而不能移植嵌入式操作系統的嵌入式系統,使用Protothreads能夠方便直觀地設計多任務程序,能夠實現用線性程序結構處理事件驅動型程序和狀態機程序,簡化了該類程序的設計。
主要特性:
- Very small RAM overhead - only two bytes per protothread and no extra stacks
- Highly portable - the protothreads library is 100% pure C and no architecture specific assembly code
- Can be used with or without an OS
- Provides blocking wait without full multi-threading or stack-switching
- Freely available under a BSD-like open source license
示例程序:
- Memory constrained systems
- Event-driven protocol stacks
- Small embedded systems
- Sensor network nodes
- Portable C applications
示例代碼:
#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); }
本文由用戶 fmms 自行上傳分享,僅供網友學習交流。所有權歸原作者,若您的權利被侵害,請聯系管理員。
轉載本站原創文章,請注明出處,并保留原始鏈接、圖片水印。
本站是一個以用戶分享為主的開源技術平臺,歡迎各類分享!