互斥鎖是一種保護(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再次試圖擁有鎖,就能成功拿到鎖了。
代碼:
#includevoid 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; }
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