国产99久久精品_欧美日本韩国一区二区_激情小说综合网_欧美一级二级视频_午夜av电影_日本久久精品视频

最新文章專題視頻專題問答1問答10問答100問答1000問答2000關(guān)鍵字專題1關(guān)鍵字專題50關(guān)鍵字專題500關(guān)鍵字專題1500TAG最新視頻文章推薦1 推薦3 推薦5 推薦7 推薦9 推薦11 推薦13 推薦15 推薦17 推薦19 推薦21 推薦23 推薦25 推薦27 推薦29 推薦31 推薦33 推薦35 推薦37視頻文章20視頻文章30視頻文章40視頻文章50視頻文章60 視頻文章70視頻文章80視頻文章90視頻文章100視頻文章120視頻文章140 視頻2關(guān)鍵字專題關(guān)鍵字專題tag2tag3文章專題文章專題2文章索引1文章索引2文章索引3文章索引4文章索引5123456789101112131415文章專題3
問答文章1 問答文章501 問答文章1001 問答文章1501 問答文章2001 問答文章2501 問答文章3001 問答文章3501 問答文章4001 問答文章4501 問答文章5001 問答文章5501 問答文章6001 問答文章6501 問答文章7001 問答文章7501 問答文章8001 問答文章8501 問答文章9001 問答文章9501
當(dāng)前位置: 首頁 - 科技 - 知識百科 - 正文

[RTT例程練習(xí)]2.6互斥鎖mutex

來源:懂視網(wǎng) 責(zé)編:小采 時間:2020-11-09 08:03:42
文檔

[RTT例程練習(xí)]2.6互斥鎖mutex

[RTT例程練習(xí)]2.6互斥鎖mutex:互斥鎖是一種保護(hù)共享資源的方法。當(dāng)一個線程擁有互斥鎖的時候,另一個線程若是等待鎖,則其就會被掛起,從而保證只有一個線程會操作共享數(shù)據(jù)。 這里的例子同樣有靜態(tài)鎖和動態(tài)鎖,其差別同之前一樣,僅僅是創(chuàng)建和刪除的方式不同。 例子中,線程2 一開始擁有
推薦度:
導(dǎo)讀[RTT例程練習(xí)]2.6互斥鎖mutex:互斥鎖是一種保護(hù)共享資源的方法。當(dāng)一個線程擁有互斥鎖的時候,另一個線程若是等待鎖,則其就會被掛起,從而保證只有一個線程會操作共享數(shù)據(jù)。 這里的例子同樣有靜態(tài)鎖和動態(tài)鎖,其差別同之前一樣,僅僅是創(chuàng)建和刪除的方式不同。 例子中,線程2 一開始擁有

互斥鎖是一種保護(hù)共享資源的方法。當(dāng)一個線程擁有互斥鎖的時候,另一個線程若是等待鎖,則其就會被掛起,從而保證只有一個線程會操作共享數(shù)據(jù)。 這里的例子同樣有靜態(tài)鎖和動態(tài)鎖,其差別同之前一樣,僅僅是創(chuàng)建和刪除的方式不同。 例子中,線程2 一開始擁有

互斥鎖是一種保護(hù)共享資源的方法。當(dāng)一個線程擁有互斥鎖的時候,另一個線程若是等待鎖,則其就會被掛起,從而保證只有一個線程會操作共享數(shù)據(jù)。

這里的例子同樣有靜態(tài)鎖和動態(tài)鎖,其差別同之前一樣,僅僅是創(chuàng)建和刪除的方式不同。

例子中,線程2 一開始擁有鎖,因為線程2的優(yōu)先級高。而后線程1一開始采用等待10個tick的方式,所以線程1等鎖的時候一定會超時。最后線程2 等1秒之后釋放鎖,然后這時線程1再次試圖擁有鎖,就能成功拿到鎖了。

代碼:

#include 

void rt_init_thread_entry(void *parameter)
{

}

static struct rt_mutex static_mutex;

static rt_mutex_t dynamic_mutex = RT_NULL;

static rt_uint8_t thread1_stack[1024];
struct rt_thread thread1;
static void rt_thread_entry1(void *parameter)
{
 rt_err_t result;
 rt_tick_t tick;
 
 /* static mutex demo */
 rt_kprintf("thread1 try to get static mutex, wait 10 ticks.\n");
 
 tick = rt_tick_get();
 
 result = rt_mutex_take(&static_mutex, 10);
 if (result == -RT_ETIMEOUT)
 {
 if (rt_tick_get() - tick != 10)
 {
 rt_mutex_detach(&static_mutex);
 return ;
 }
 }
 else
 {
 rt_kprintf("thread1 take a static mutex, failed.\n");
 rt_mutex_detach(&static_mutex);
 return ;
 }
 
 /* wait forever */
 rt_kprintf("thread1 try to get static mutex, wait forever.\n");
 result = rt_mutex_take(&static_mutex, RT_WAITING_FOREVER);
 if (result != RT_EOK)
 {
 rt_kprintf("thread1 take a static mutex, failed.\n");
 rt_mutex_detach(&static_mutex);
 return ;
 }
 
 rt_kprintf("thread1 take a static mutex, done.\n");
 
 rt_mutex_detach(&static_mutex);
 
 /* dynamic mutex test */
 rt_kprintf("thread1 try to get dynamic mutex, wait 10 ticks.\n");
 
 tick = rt_tick_get();
 
 result = rt_mutex_take(dynamic_mutex, 10);
 if (result == -RT_ETIMEOUT)
 {
 if (rt_tick_get() - tick != 10)
 {
 rt_mutex_delete(dynamic_mutex);
 return ;
 }
 rt_kprintf("thread1 take dynamic mutex timeout.\n");
 }
 else
 {
 rt_kprintf("thread1 take a dynamic mutex, failed.\n");
 rt_mutex_delete(dynamic_mutex);
 return ;
 }
 
 rt_kprintf("thread1 try to take dynamic mutex, wait forever.\n");
 result = rt_mutex_take(dynamic_mutex, RT_WAITING_FOREVER);
 if (result != RT_EOK)
 {
 rt_kprintf("thread1 take a dynamic mutex, failed.\n");
 rt_mutex_delete(dynamic_mutex);
 return ;
 }
 
 rt_kprintf("thread1 take a dynamic mutex,done.\n");
 rt_mutex_delete(dynamic_mutex);
}

static rt_uint8_t thread2_stack[1024];
struct rt_thread thread2;
static void rt_thread_entry2(void *parameter)\
{
 //rt_err_t result;
 //rt_tick_t tick;
 
 rt_kprintf("thread2 try to take static mutex.\n");
 rt_mutex_take(&static_mutex, 10);
 rt_kprintf("thread2 got static mutex.\n");
 rt_thread_delay(RT_TICK_PER_SECOND);
 rt_kprintf("thread2 release static mutex.\n");
 rt_mutex_release(&static_mutex);
 
 rt_kprintf("thread2 try to take dynamic mutex.\n");
 rt_mutex_take(dynamic_mutex, 10);
 rt_kprintf("thread2 got dynamic mutex.\n");
 rt_thread_delay(RT_TICK_PER_SECOND);
 rt_kprintf("thread2 release dynamic mutex.\n");
 rt_mutex_release(dynamic_mutex);
}

int rt_application_init()
{
 //rt_thread_t init_thread;
 rt_err_t result;
 
 result = rt_mutex_init(&static_mutex, "smutex", RT_IPC_FLAG_FIFO);
 if (result != RT_EOK)
 {
 rt_kprintf("init static mutex failed.\n");
 return -1;
 }
 dynamic_mutex = rt_mutex_create("dmutex", RT_IPC_FLAG_FIFO);
 if (dynamic_mutex == RT_NULL)
 {
 rt_kprintf("create dynamic mutex failed.\n");
 return -1;
 }

 rt_thread_init(&thread1,
 "thread1",
 rt_thread_entry1,
 RT_NULL,
 &thread1_stack[0],
 sizeof(thread1_stack),11,5);
 rt_thread_startup(&thread1);


 rt_thread_init(&thread2,
 "thread2",
 rt_thread_entry2,
 RT_NULL,
 &thread2_stack[0],
 sizeof(thread2_stack),10,5);
 rt_thread_startup(&thread2);
 return 0;
}

結(jié)果:
thread2 try to get static mutex
thread2 got static mutex
thread1 try to get static mutex, wait 10 ticks.
thread1 take static mutex timeout
thread1 try to get static mutex, wait forever.
thread2 release static mutex
thread2 try to get dynamic mutex
thread2 got dynamic mutex
thread1 take a staic mutex, done.
thread1 try to get dynamic mutex, wait 10 ticks.
thread1 take dynamic mutex timeout
thread1 try to get dynamic mutex, wait forever.
thread2 release dynamic mutex
thread1 take a dynamic mutex, done.

聲明:本網(wǎng)頁內(nèi)容旨在傳播知識,若有侵權(quán)等問題請及時與本網(wǎng)聯(lián)系,我們將在第一時間刪除處理。TEL:177 7030 7066 E-MAIL:11247931@qq.com

文檔

[RTT例程練習(xí)]2.6互斥鎖mutex

[RTT例程練習(xí)]2.6互斥鎖mutex:互斥鎖是一種保護(hù)共享資源的方法。當(dāng)一個線程擁有互斥鎖的時候,另一個線程若是等待鎖,則其就會被掛起,從而保證只有一個線程會操作共享數(shù)據(jù)。 這里的例子同樣有靜態(tài)鎖和動態(tài)鎖,其差別同之前一樣,僅僅是創(chuàng)建和刪除的方式不同。 例子中,線程2 一開始擁有
推薦度:
標(biāo)簽: 一種 互斥 練習(xí)
  • 熱門焦點

最新推薦

猜你喜歡

熱門推薦

專題
Top
主站蜘蛛池模板: 国产一二三区在线 | 亚洲乱码中文论理电影 | 日本成人一区 | 在线亚洲欧美日韩 | 91精品国产91久久久久久最新 | 亚洲欧美国产精品专区久久 | 精品国产成人综合久久小说 | 韩日一区二区 | 日韩精品一区二区三区视频 | 欧美日韩亚洲另类 | 99久久国产综合精品成人影院 | 国产欧美日韩第一页 | 日本另类αv欧美另类aⅴ | 欧美一区二区三区视频 | 精品日韩在线观看 | 亚洲色图欧美 | 久久久久久综合成人精品 | 久久国产精品免费一区二区三区 | 精品国产欧美一区二区五十路 | 亚洲爱爱网站 | 一区二区三区在线视频播放 | 午夜看一级特黄a大片黑 | 真人一级一级毛片免费观看 | 国内一区二区三区精品视频 | 国产欧美日韩视频在线观看 | 欧美天天 | 国内精品伊人久久久久 | 五月婷婷啪啪 | 日韩第二页| 91久久精品国产一区二区 | 中文字幕精品一区二区精品 | 成人精品一区二区三区 | 亚洲首页在线观看 | 日韩欧美一区二区三区中文精品 | 91原创国产 | 99久久精品国产亚洲 | 在线观看国产一区二区三区 | 日韩欧美视频 | 91精品一区二区三区久久久久 | 免费精品在线观看 | 欧美激情在线播放一区二区三区 |