Chinaunix首页 | 论坛 | 博客
  • 博客访问: 234907
  • 博文数量: 31
  • 博客积分: 0
  • 博客等级: 民兵
  • 技术积分: 296
  • 用 户 组: 普通用户
  • 注册时间: 2016-06-22 11:52
文章分类

全部博文(31)

文章存档

2018年(3)

2017年(11)

2016年(12)

2015年(5)

我的朋友

分类: C/C++

2017-09-10 15:55:08

每次都是有问题就让我帮忙解决,有这样的队友,不知道该喜该忧,不过也好,解决的问题越多,懂得越多,实现起需求来就越得心应手。问题大概是这样的,主线程新建了10个线程,新建完之后,主线程希望等所有新建的线程都开始执行了,再往下走。一般主线程sleep  几秒后再往下走,都能达到这样的效果,但是这样的代码要是出现在真正的项目中,那就显得太糟糕了。测试了一下,这种场景用条件变量是能实现的。代码如下:


点击(此处)折叠或打开

  1. #include<stdio.h>
  2. #include<stdlib.h>
  3. #include <pthread.h>
  4. #include <errno.h>
  5. #include <string.h>

  6. #define N_THREAD 10

  7. static int thread_count = 0;
  8. static pthread_cond_t thread_cond;
  9. static pthread_mutex_t thread_lock;


  10. void *thread_fun(void *arg)
  11. {
  12.         pthread_mutex_lock(&thread_lock);
  13.                 if(++thread_count>= N_THREAD){
  14.                 // printf("send signal\n");
  15.                 // pthread_cond_signal(&thread_cond);
  16.                 }
  17.         printf("start thread-%d pid=%d\n",thread_count,pthread_self());
  18.         pthread_mutex_unlock(&thread_lock);
  19.     
  20.         pthread_exit(NULL);
  21. }

  22. int main(int argc,char **argv)
  23. {
  24.         pthread_t pid[N_THREAD];
  25.         int err;
  26.         void *result;
  27.         int i = 0;
  28.     
  29.         pthread_cond_init(&thread_cond, NULL);
  30.         pthread_mutex_init(&thread_lock, NULL);
  31.         for(i=0;i<N_THREAD;i++) {
  32.                 err = pthread_create(&pid[i],NULL,thread_fun,"hello");
  33.                 pthread_detach(pid[i]);
  34.                 printf("create thread-%d,pid=%d\n",i+1,pid[i]);
  35.                 if(err !=0){
  36.                         printf("create thread fail ,%s\n",strerror(errno));
  37.                 }
  38.         }

  39.         /*wait all thread start */
  40.      /* pthread_mutex_lock(&thread_lock);
  41.                 while(thread_count < N_THREAD){
  42.                         pthread_cond_wait(&thread_cond,&thread_lock);
  43.                 }
  44.         pthread_mutex_unlock(&thread_lock);*/

  45.         printf("all thread start \n ");

  46.         sleep(1);   
  47.         pthread_cond_destroy(&thread_cond);
  48.         pthread_mutex_destroy(&thread_lock);
  49.         return 0;

  50. }
以上代码运行结果是不确定的,"all thread start " 这一句话的运行时序是随机的,如下:
执行第一次结果:
create thread-1,pid=1082132800
start thread-1 pid=1082132800
create thread-2,pid=1090525504
create thread-3,pid=1098918208
start thread-2 pid=1090525504
start thread-3 pid=1098918208
create thread-4,pid=1107310912
start thread-4 pid=1107310912
create thread-5,pid=1115703616
create thread-6,pid=1124096320
create thread-7,pid=1132489024
create thread-8,pid=1082132800
start thread-5 pid=1115703616
create thread-9,pid=1140881728
create thread-10,pid=1090525504
all thread start 
 start thread-6 pid=1132489024
start thread-7 pid=1124096320
start thread-8 pid=1090525504
start thread-9 pid=1140881728
start thread-10 pid=1082132800

执行第二次结果:
create thread-1,pid=1082132800
create thread-2,pid=1090525504
create thread-3,pid=1098918208
create thread-4,pid=1107310912
create thread-5,pid=1115703616
create thread-6,pid=1124096320
create thread-7,pid=1132489024
create thread-8,pid=1140881728
create thread-9,pid=1149274432
create thread-10,pid=1157667136
all thread start 
 start thread-1 pid=1157667136
start thread-2 pid=1115703616
start thread-3 pid=1082132800
start thread-4 pid=1090525504
start thread-5 pid=1098918208
start thread-6 pid=1107310912
start thread-7 pid=1124096320
start thread-8 pid=1132489024
start thread-9 pid=1140881728
start thread-10 pid=1149274432
这样的随机结果不是我们想要的,我们是希望所有新建的线程开始执行,主线程才往下走了。

把上面代码中的18-19,46-50 行的注释打开,每一次的执行结果"all thread start" 这一句都是最后打印的,如下:
create thread-1,pid=1082132800
start thread-1 pid=1082132800
create thread-2,pid=1090525504
create thread-3,pid=1098918208
start thread-2 pid=1090525504
create thread-4,pid=1107310912
start thread-3 pid=1098918208
create thread-5,pid=1115703616
start thread-4 pid=1115703616
start thread-5 pid=1107310912
create thread-6,pid=1124096320
start thread-6 pid=1124096320
create thread-7,pid=1132489024
start thread-7 pid=1132489024
create thread-8,pid=1140881728
create thread-9,pid=1149274432
create thread-10,pid=1157667136
start thread-8 pid=1140881728
start thread-9 pid=1149274432
send signal
start thread-10 pid=1157667136
all thread start 

当主线程新建完所有线程后,执行到47 行的时候,如果所有新建的线程都开始执行了,就不会跳到while 循环里面,阻塞在函数pthread_cond_wait上 ,只要有一个新建的线程没有开始执行条件thread_count 就成立,主线程会阻塞在phtread_cond_wait上,当最后一个新建的线程开始执行的时候,17行的条件会满足,从而调用pthread_cond_signal ,唤醒阻塞的线程,继续执行,从而达到等待所有新建线程都开始执行后,主线程再往下走的效果

ps:从上面的执行结果也能看出,先创建的线程,不一定是先执行线程处理函数的。






阅读(1757) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~