免费注册 查看新帖 |

Chinaunix

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

求教:创建互斥锁多线程遇到的问题! [复制链接]

论坛徽章:
0
跳转到指定楼层
1 [收藏(0)] [报告]
发表于 2011-07-06 00:15 |只看该作者 |倒序浏览
本人新手,向各位牛人求教!
以下实例中创建3个线程,为了更好的描述线程间的并行执行,让3个线程重用一个执行函数。每个线程都有五次循环(可以看成5个小任务),每次循环之间会随机等待1~10s的时间。
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include <pthread.h>

  4. #define  THREAD_NUMBER                      3              /* 线程数*/
  5. #define  REPEAT_NUMBER                      3               /* 每个线程中的小任务数*/
  6. #define  DELAY_TIME_LEVELS              10.0          /* 小任务间的最大时间间隔*/

  7. /*声明一个全局锁*/
  8. pthread_mutex_t mutex;


  9. /*线程函数例程*/
  10. void *thrd_func(void *arg)
  11. {
  12.         int thrd_num = (int)arg;
  13.         int delay_time = 0;
  14.         int res = 0;
  15.         int count = 0;

  16.         /*互斥锁上锁*/
  17.         res = pthread_mutex_lock(&mutex);

  18.         if(res)
  19.         {
  20.                 printf("Thread %d Lock failed!\n",thrd_num);
  21.                 pthread_exit(NULL);
  22.         }

  23.         printf("Thread %d is Starting!\n",thrd_num);

  24.         for(count = 0; count < REPEAT_NUMBER; count++)
  25.         {
  26.             delay_time = (int)(rand()*DELAY_TIME_LEVELS/(RAND_MAX))+1;
  27.                
  28.              sleep(delay_time);

  29.             printf("\tThread%d: job %d delay = %d\n",thrd_num,count,delay_time);
  30.         }

  31.         printf("Thread %d is finished\n",thrd_num);

  32.         pthread_exit(NULL);
  33. }

  34. int main(void)
  35. {
  36.         int loop = 0;
  37.         int res = 0;
  38.        
  39.         void *thrd_res = NULL;
  40.        
  41.         pthread_t thread[THREAD_NUMBER] = {0};

  42.         srand(time(NULL));

  43.          /*互斥锁初始化*/
  44.         pthread_mutex_init(&mutex,NULL);

  45.         for(loop = 0; loop < THREAD_NUMBER; loop++)
  46.         {
  47.                  /* 创建多线程*/
  48.                 res = pthread_create(&thread[loop],NULL,thrd_func,(void*)loop);
  49.                          
  50.                 if(0 != res)
  51.                 {
  52.                        printf("Create thread %d fail!\n",loop);
  53.                      exit(res);
  54.                 }
  55.         }

  56.         printf("Create threads success!\nWaiting for threads to finish......\n");

  57.         for(loop = 0; loop < THREAD_NUMBER; loop++)
  58.         {
  59.                 /* 等待多线程结束*/
  60.                 res = pthread_join(thread[loop],&thrd_res);
  61.                
  62.                 if(!res)
  63.                 {
  64.                         printf("Thread %d joined!\n",loop);
  65.                 }
  66.                 else
  67.                 {
  68.                         printf("Thread %d join failed!\n",loop);
  69.                 }

  70.                   /*互斥锁解锁*/
  71.                 pthread_mutex_unlock(&mutex);
  72.         }

  73.         /*消除互斥锁*/
  74.         pthread_mutex_destroy(&mutex);

  75.         return 0;
  76. }
复制代码
但是运行下来是以下情况:

[root@chengxiaoquan pthread_mutex]# gcc pthread_mutex.c -o pthread_mutex -lpthread
[root@chengxiaoquan pthread_mutex]# ./pthread_mutex
Create threads success!
Waiting for threads to finish......
Thread 2 is Starting!
        Thread2: job 0 delay = 8
        Thread2: job 1 delay = 4
        Thread2: job 2 delay = 3
Thread 2 is finished


运行到这里就不动了!!!

怎么只有线程2????而且pthread2并没有join,为什么呢?本人新手,向各位牛人求教!

论坛徽章:
1
天蝎座
日期:2013-12-06 18:23:58
2 [报告]
发表于 2011-07-06 08:48 |只看该作者
本帖最后由 crazyhadoop 于 2011-07-06 09:34 编辑

一个线程请求了锁,也不释放,别的线程基本无解吧?你看看是不是死锁了。



/*互斥锁解锁*/
                pthread_mutex_unlock(&mutex); 放到线程函数里面是不是好点?
您需要登录后才可以回帖 登录 | 注册

本版积分规则 发表回复

  

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

清除 Cookies - ChinaUnix - Archiver - WAP - TOP