Chinaunix首页 | 论坛 | 博客
  • 博客访问: 1479700
  • 博文数量: 148
  • 博客积分: 2234
  • 博客等级: 大尉
  • 技术积分: 3225
  • 用 户 组: 普通用户
  • 注册时间: 2012-05-17 21:34
个人简介

未来很长。

文章存档

2017年(7)

2016年(4)

2015年(1)

2014年(6)

2013年(31)

2012年(99)

分类: LINUX

2012-08-05 18:48:12


点击(此处)折叠或打开

  1. #include<stdio.h>
  2. #include<unistd.h>
  3. #include<stdlib.h>
  4. #include<string.h>
  5. #include<pthread.h>
  6. #include<semaphore.h>
  7. void *thread_function(void *arg);
  8. sem_t sem_bin;
  9. #define WORK_SIZE 1024
  10. char work_area[WORK_SIZE];
  11. int main()
  12. {
  13.     int res;
  14.     pthread_t thread;
  15.     void *thread_result;
  16.     res=sem_init(&sem_bin,0,0);
  17.     if(res!=0)
  18.     {
  19.         perror("semphore failed\n");
  20.         exit(0);
  21.     }
  22.     res=pthread_create(&thread,NULL,thread_function,NULL);
  23.     if(res!=0)
  24.     {
  25.         perror("pthread create failed\n");
  26.         exit(0);
  27.     }
  28.     printf("input some text. enter 'end' to finishi\n");
  29.     while((strncmp("end",work_area,3))!=0)
  30.     {
  31.         fgets(work_area,WORK_SIZE,stdin);
  32.         sem_post(&sem_bin);
  33.     }
  34.     printf("\nwaiting for thread to finish\n");
  35.     res=pthread_join(thread,&thread_result);
  36.     if(res!=0)
  37.     {
  38.         perror("thread joined failed\n");
  39.         exit(EXIT_FAILURE);
  40.     }
  41.     printf("thread exit returned %s\n",(char *)thread_result);
  42.     sem_destroy(&sem_bin);
  43.     exit(EXIT_SUCCESS);
  44. }
  45. void *thread_function(void *arg)
  46. {
  47.     sem_wait(&sem_bin);
  48.     while(strncmp("end",work_area,3)!=0)
  49.     {
  50.         printf("you input %d characters \n",strlen(work_area)-1);
  51.         sem_wait(&sem_bin);
  52.     }
  53.     pthread_exit("thread exit nomaly!");    
  54.         
  55. }

初始化信号量时,我们把它的值设置为0。这样,在线程函数启动时,sem_wait函数调用就会阻塞并等待信号量变为非零值。

在主线程中,我们等待直到有文本输入,然后调用sem_post增加信号量的值,这将立刻令另一个线程从sem_wait的等待中返回并开始执行。在统计完字符个数之后,它再次调用sem_wait并再次被阻塞,直到主线程再次调用sem_post增加信号量的值为止。


点击(此处)折叠或打开

  1. #include<stdio.h>
  2. #include<unistd.h>
  3. #include<stdlib.h>
  4. #include<string.h>
  5. #include<pthread.h>
  6. #include<semaphore.h>
  7. void *thread_function(void *arg);
  8. pthread_mutex_t work_mutex;
  9. #define WORK_SIZE 1024
  10. char work_area[WORK_SIZE];
  11. int time_to_exit=0;
  12. int main()
  13. {
  14.     int res;
  15.     pthread_t thread;
  16.     void *thread_result;
  17.     res=pthread_mutex_init(&work_mutex,NULL);
  18.     if(res!=0)
  19.     {
  20.         perror("semphore failed\n");
  21.         exit(EXIT_FAILURE);
  22.     }
  23.     res=pthread_create(&thread,NULL,thread_function,NULL);
  24.     if(res!=0)
  25.     {
  26.         perror("pthread create failed\n");
  27.         exit(EXIT_FAILURE);
  28.     }
  29.     pthread_mutex_lock(&work_mutex);
  30.     printf("input some text. enter 'end' to finishi\n");
  31.     while(!time_to_exit)
  32.     {
  33.         fgets(work_area,WORK_SIZE,stdin);
  34.         pthread_mutex_unlock(&work_mutex);
  35.         while(1)
  36.         {
  37.             pthread_mutex_lock(&work_mutex);
  38.             if(work_area[0]!='\0')
  39.             {
  40.                 pthread_mutex_unlock(&work_mutex);
  41.                 sleep(1);
  42.             }
  43.             else
  44.                     break;
  45.         }
  46.     }
  47.     pthread_mutex_unlock(&work_mutex);
  48.     printf("\nwaiting for thread to finish\n");
  49.     res=pthread_join(thread,&thread_result);
  50.     if(res!=0)
  51.     {
  52.         perror("thread joined failed\n");
  53.         exit(EXIT_FAILURE);
  54.     }
  55.     printf("thread exit returned %s\n",(char *)thread_result);
  56.     pthread_mutex_destroy(&work_mutex);
  57.     exit(EXIT_SUCCESS);
  58. }
  59. void *thread_function(void *arg)
  60. {
  61.     sleep(1);
  62.     pthread_mutex_lock(&work_mutex);
  63.     while(strncmp("end",work_area,3)!=0)
  64.     {
  65.         printf("you input %d characters \n",strlen(work_area)-1);
  66.         work_area[0]='\0';
  67.         pthread_mutex_unlock(&work_mutex);
  68.         sleep(1);
  69.         pthread_mutex_lock(&work_mutex);
  70.     }
  71.     time_to_exit=1;
  72.     work_area[0]='\0';
  73.     pthread_mutex_unlock(&work_mutex);
  74.     
  75.     pthread_exit("thread exit nomaly!");    
  76.         
  77. }

这段代码的完成的功能和上例基本相同,因此不再过多解析。值得提出的是,这段代码中通过轮询的方法获得结果通常并不是好的编程方式,在实际的编程中,我们应该尽可能用信号量来避免出现这种情况。这里的代码只是用作示例的目的。


点击(此处)折叠或打开

  1. #include<stdio.h>
  2. #include<unistd.h>
  3. #include<stdlib.h>
  4. #include<pthread.h>
  5. #define NUM_THREADS 6//定义线程个数
  6. void *thread_function(void *arg);
  7. int main()
  8. {
  9.     int res;
  10.     pthread_t thread[NUM_THREADS];
  11.     int lots_of_threads;
  12.     for(lots_of_threads=0;lots_of_threads<NUM_THREADS;lots_of_threads++)
  13.     {
  14.         res=pthread_create(&thread[lots_of_threads],NULL,thread_function,(void *)&lots_of_threads);
  15.         if(res!=0)
  16.         {
  17.             perror("create thread failed\n");
  18.             exit(EXIT_FAILURE);
  19.         }
  20.         sleep(1);
  21.     }
  22.     printf("waiting for threads to finishi\n");
  23.     for(lots_of_threads=NUM_THREADS-1;lots_of_threads>=0;lots_of_threads--)
  24.     {
  25.         res=pthread_join(thread[lots_of_threads],NULL);
  26.         if(res==0)
  27.         {
  28.             printf("Picked u a thread\n");
  29.         }
  30.         else
  31.         {
  32.             perror("pthread joined failed\n");
  33.         }
  34.     }
  35.     printf("ALL done\n");
  36.     exit(EXIT_SUCCESS);
  37. }
  38. void *thread_function(void *arg)
  39. {
  40.     int my_number=*(int *)arg;
  41.     int rand_num;
  42.     printf("thread_function is .Argument was %d\n",my_number);
  43.     sleep(2);
  44.     printf("After sleep(2) Agument is %d\n",my_number);
  45.     rand_num=1+(int)(9.0*rand()/(RAND_MAX+1.0));
  46.     sleep(rand_num);
  47.     printf("Bye from %d\n",my_number);
  48.     pthread_exit(NULL);
  49. }
这个程序中,我们创建了一个线程ID的数组,然后循环创建多个线程,创建出的线程等待一段随机时间后退出运行。在主线程中我们等待合并这些子线程,但并不是以创建它们的顺序来等待的。

我们看到在主线程中循环创建子线程的时候,有一个sleep(1),当我们把这条语句去掉以后会是什么结果呢?结果变得很是奇怪?我们分析一下以下结果:


我们看到出现个数字6,我怎么也想不明白怎么会出现个数字6呢?

我的理解是这样的,首先这些线程跟主线程以及它们之间相互都是并发交替执行的,所以当把sleep(1)去掉以后,由于CPU执行很快,当主线程当好执行for(lots_of_threads=NUM_THREADS-1;lots_of_threads>=0;lots_of_threads--)这条语句的时候,此时把lots_of_threads赋值为了5,但是此时第五个线程刚好要执行,把当前的lots_of_threads++后调用线程创建函数把这个值传了过去就出现了上面的结果。不过这仅仅是我自己个人的推理,也许不准确,再好好研究研究吧,对于多线程编程来说,时间上的控制是最难掌握的。

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