线程让出实验【RT-Thread学习笔记 4】
API: rt_thread_yield
線程函數(shù)中調(diào)用,本線程釋放MCU。如果此時(shí)有別的相同優(yōu)先級(jí)的任務(wù)整處于等待狀態(tài),將獲得MCU使用權(quán)。
線程讓出就是給OS增加一個(gè)任務(wù)調(diào)度的機(jī)會(huì)。
創(chuàng)建兩個(gè)線程,觀察他們的結(jié)果:
//線程讓出試驗(yàn) void yield_test1(void* parameter) {rt_uint32_t count = 0;while(1){rt_kprintf("thread test1 count:%d\n",count++);rt_thread_yield();} } void yield_test2(void* parameter) {rt_uint32_t count = 0;while(1){rt_kprintf("thread test2 count:%d\n",count++);rt_thread_yield();} }啟動(dòng)他們:
//線程讓出實(shí)驗(yàn),兩個(gè)線程優(yōu)先級(jí)一樣。否則在給一次調(diào)度機(jī)會(huì)也是高優(yōu)先級(jí)的任務(wù)使用MCUtid2 = rt_thread_create("yield1",yield_test1,RT_NULL,2048,10,5);if(tid2 != RT_NULL)rt_thread_startup(tid2);tid2 = rt_thread_create("yield2",yield_test2,RT_NULL,2048,10,5);if(tid2 != RT_NULL)rt_thread_startup(tid2);?看見兩個(gè)線程輪流輸出:
\ | /
- RT - Thread Operating System
/ | \ 2.0.0 build Aug 29 2014
2006 - 2013 Copyright by rt-thread team
thread test1 count:0
thread test2 count:0
thread test1 count:1
thread test2 count:1
thread test1 count:2
thread test2 count:2
thread test1 count:3
thread test2 count:3
thread test1 count:4
thread test2 count:4
thread test1 count:5
thread test2 count:5
……..
如果沒有線程讓出的操作,情況將是等一個(gè)線程時(shí)間片結(jié)束之后,才會(huì)輪到另一個(gè)線程輸出。不會(huì)是輪流輸出了
總結(jié)
以上是生活随笔為你收集整理的线程让出实验【RT-Thread学习笔记 4】的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 软考复习之路—组成原理
- 下一篇: 求 s=a+aa+ aaa+ aaaa