免费注册 查看新帖 |

Chinaunix

  平台 论坛 博客 文库
最近访问板块 发新帖
查看: 1550 | 回复: 6
打印 上一主题 下一主题

新手、 多线程编程: 拿不到锁的问题 真心 求高人指点 [复制链接]

论坛徽章:
1
2015年辞旧岁徽章
日期:2015-03-03 16:54:15
跳转到指定楼层
1 [收藏(0)] [报告]
发表于 2011-05-10 10:39 |只看该作者 |倒序浏览
//谁休眠 谁拿锁???? 这是我整个疑问  


程序不短 但是几乎都是重复的代码、语法简单、 希望您能耐心的看下去。

代码中 在有疑问的地方、我注了注释
  1. #include "test_mutex.h"


  2. static unityos_mutex_t * test_mutex = NULL;


  3. void mutex_in()
  4. {


  5.         pthread_t  tid6, tid7;
  6.     int rc6 = 0, rc7 = 0;
  7.     int i;

  8.         test_mutex = unityos_mutex_create();
  9.        
  10.    rc6 = pthread_create(&tid6, NULL, mutex_write_one, NULL);

  11.     if ( rc6 != NULL)
  12.     {
  13.         printf("thread_tran_msg create\n");
  14.     }

  15.     rc7 = pthread_create(&tid7, NULL, mutex_write_two, NULL);

  16.     if ( rc7 != NULL)
  17.     {
  18.         printf("thread_tran_msg create\n");
  19.     }

  20.         pthread_join(tid6, NULL);
  21.     pthread_join(tid7, NULL);
  22.         unityos_mutex_destroy(test_mutex);
  23.    
  24. }

  25. /*****************************************************************************
  26. 函 数 名  : mutex_write_one
  27. 功能描述  : TASK_3 用于互斥读结构体
  28. 输入参数  : void* args  
  29. 输出参数  : 无
  30. 返 回 值  : void
  31. 主调函数  :
  32. 被调函数  :

  33. 修改历史      :
  34.   1.日    期   : 2011年4月28日
  35.     作    者   : Eli.Liu
  36.     修改内容   : 新生成函数

  37. *****************************************************************************/

  38. void *mutex_write_one(void* args)
  39. {
  40.        
  41.     int i, count;
  42.     count = 1;

  43.     while(count++ < 5)
  44.     {
  45.       
  46.         unityos_mutex_lock(test_mutex);
  47.                 printf("******************one\n");
  48.         for(i = 0; i < 3; i++)
  49.         {
  50.             strcpy(per_inf[i].birthday, "1999-01-01");
  51.             strcpy(per_inf[i].name, "lisi");
  52.         }
  53.                 mutex_print();
  54.         unityos_mutex_unlock(test_mutex);
  55.         sleep(1);  //如果休眠  write_one就可以持续那锁、一直等到 while循环结束write_two才能拿到  
  56.                
  57.         
  58.     }
  59.         return 0;
  60.        

  61. }



  62. /*****************************************************************************
  63. 函 数 名  : mutex_write_two
  64. 功能描述  : TASK_3 用于互斥读结构体
  65. 输入参数  : void* args  
  66. 输出参数  : 无
  67. 返 回 值  : void
  68. 主调函数  :
  69. 被调函数  :

  70. 修改历史      :
  71.   1.日    期   : 2011年4月28日
  72.     作    者   : Eli.Liu
  73.     修改内容   : 新生成函数

  74. *****************************************************************************/
  75. void * mutex_write_two(void* args)
  76. {

  77.     int i, count;
  78.     count = 1;

  79.     while(count++ < 5)
  80.     {
  81.      
  82.         unityos_mutex_lock(test_mutex);
  83.                 printf("******************two\n");

  84.         for(i = 0; i < 3; i++)
  85.         {
  86.             strcpy(per_inf[i].birthday, "2012-12-22");
  87.             strcpy(per_inf[i].name, "xiaoliu");
  88.         }
  89.                 mutex_print();
  90.         unityos_mutex_unlock(test_mutex);
  91.                
  92.                 sleep(1);  //如果休眠  write_two就可以持续那锁、一直等到 while循环结束write_one才能拿到
  93.                 //2、如果两个线程都休眠 则交替拿锁?
  94.                 //3、如果两个线程都不休眠 则 write_one持续拿锁 、直到其while循环结束
  95.                
  96.       
  97.     }
  98.         return 0;

  99. }


  100. void mutex_print()
  101. {
  102.     int i, count;
  103.     count = 1;

  104.     while(count++ < 5)
  105.     {
  106.         printf("name\t\tbirthday\n");

  107.         for(i = 0; i < 3; i++)
  108.         {
  109.             printf("%s\t", per_inf[i].name);
  110.             printf("%s\n", per_inf[i].birthday);
  111.         }

  112.         sleep(1);
  113.     }
  114.        

  115. }
复制代码

论坛徽章:
1
2015年辞旧岁徽章
日期:2015-03-03 16:54:15
2 [报告]
发表于 2011-05-10 10:41 |只看该作者
辛苦了

论坛徽章:
1
2015年辞旧岁徽章
日期:2015-03-03 16:54:15
3 [报告]
发表于 2011-05-10 13:15 |只看该作者
必须要delay以下  否则 真的不释放……

论坛徽章:
0
4 [报告]
发表于 2011-05-10 13:23 |只看该作者
真看不懂

论坛徽章:
1
2015年辞旧岁徽章
日期:2015-03-03 16:54:15
5 [报告]
发表于 2011-05-10 13:44 |只看该作者
回复 4# taojie2000


    你说女生IT做什么好  运维? 测试?

论坛徽章:
0
6 [报告]
发表于 2011-05-10 13:53 |只看该作者
回复 5# lml1987236


          你真的是MM ?  

     女的很多做程序测试

论坛徽章:
1
2015年辞旧岁徽章
日期:2015-03-03 16:54:15
7 [报告]
发表于 2011-05-11 19:47 |只看该作者
回复 6# taojie2000


    这问题 你纠结 多久了…… 纯爷们
您需要登录后才可以回帖 登录 | 注册

本版积分规则 发表回复

  

北京盛拓优讯信息技术有限公司. 版权所有 京ICP备16024965号-6 北京市公安局海淀分局网监中心备案编号:11010802020122 niuxiaotong@pcpop.com 17352615567
未成年举报专区
中国互联网协会会员  联系我们:huangweiwei@itpub.net
感谢所有关心和支持过ChinaUnix的朋友们 转载本站内容请注明原作者名及出处

清除 Cookies - ChinaUnix - Archiver - WAP - TOP