视频1 视频21 视频41 视频61 视频文章1 视频文章21 视频文章41 视频文章61 推荐1 推荐3 推荐5 推荐7 推荐9 推荐11 推荐13 推荐15 推荐17 推荐19 推荐21 推荐23 推荐25 推荐27 推荐29 推荐31 推荐33 推荐35 推荐37 推荐39 推荐41 推荐43 推荐45 推荐47 推荐49 关键词1 关键词101 关键词201 关键词301 关键词401 关键词501 关键词601 关键词701 关键词801 关键词901 关键词1001 关键词1101 关键词1201 关键词1301 关键词1401 关键词1501 关键词1601 关键词1701 关键词1801 关键词1901 视频扩展1 视频扩展6 视频扩展11 视频扩展16 文章1 文章201 文章401 文章601 文章801 文章1001 资讯1 资讯501 资讯1001 资讯1501 标签1 标签501 标签1001 关键词1 关键词501 关键词1001 关键词1501 专题2001
[RTT例程练习]2.6互斥锁mutex
2020-11-09 08:03:42 责编:小采
文档

互斥锁是一种保护共享资源的方法。当一个线程拥有互斥锁的时候,另一个线程若是等待锁,则其就会被挂起,从而保证只有一个线程会操作共享数据。 这里的例子同样有静态锁和动态锁,其差别同之前一样,仅仅是创建和删除的方式不同。 例子中,线程2 一开始拥有

互斥锁是一种保护共享资源的方法。当一个线程拥有互斥锁的时候,另一个线程若是等待锁,则其就会被挂起,从而保证只有一个线程会操作共享数据。

这里的例子同样有静态锁和动态锁,其差别同之前一样,仅仅是创建和删除的方式不同。

例子中,线程2 一开始拥有锁,因为线程2的优先级高。而后线程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;
}

结果:
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.

下载本文
显示全文
专题