zoukankan      html  css  js  c++  java
  • [RTT例程练习] 2.6 互斥锁 mutex

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

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

    例子中,线程2 一开始拥有锁,因为线程2的优先级高。而后线程1一开始采用等待10个tick的方式,所以线程1等锁的时候一定会超时。最后线程2 等1秒之后释放锁,然后这时线程1再次试图拥有锁,就能成功拿到锁了。

    代码:

    #include <rtthread.h>
    
    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.


  • 相关阅读:
    Oracle实用SQL语句
    关键字过滤(转载)
    最简单的Cache
    .net中下载文件的方法(转载)
    为SharePoint 2013配置Office Web Apps
    使用Visual Studio 部署SharePoint时提示“路径中具有非法字符”
    TFS 2012 在IE 10(Windows 8)显示英文的解决方案
    为什么使用TFS 2012进行源代码管理——TFS 2012使用简介(一)
    SharePoint 2010 在WebPart页面上调用扩展方法报方法未定义的解决方案
    TreeView结合UpdatePanel使用时,SelectedNodeStyle不生效的解决方案
  • 原文地址:https://www.cnblogs.com/lyyyuna/p/4123937.html
Copyright © 2011-2022 走看看