Chinaunix首页 | 论坛 | 博客
  • 博客访问: 1480822
  • 博文数量: 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:18:40


点击(此处)折叠或打开

  1. #include<stdio.h>
  2. #include<pthread.h>
  3. #include<unistd.h>
  4. #include<stdlib.h>
  5. #include<string.h>
  6. void *thread_function(void *arg);
  7. char message[]="Hello World!";
  8. void main()
  9. {
  10.     pthread_t thread;
  11.     int res;
  12.     void *thread_result;
  13.     res=pthread_create(&thread,NULL,thread_function,(void *)message);
  14.     if(res!=0)
  15.     {
  16.         perror("thread creation failed\n");
  17.         exit(EXIT_FAILURE);
  18.     }
  19.     printf("waiting for thread to finish..\n");
  20.     res=pthread_join(thread,&thread_result);
  21.     if(res!=0)
  22.     {
  23.         perror("Threa join failed\n");
  24.         exit(EXIT_FAILURE);
  25.     }
  26.     printf("thread joined,it returned %s\n",(char *)thread_result);
  27.     printf("message is now %s\n",message);
  28.     exit(EXIT_FAILURE);
  29. }
  30. void *thread_function(void *arg)
  31. {
  32.     
  33.     printf("thread function is running,Argument was %s\n",(char *)arg);
  34.     sleep(3);
  35.     strcpy(message,"Bye!");
  36.     pthread_exit("Thank you for the CPU time");
  37. }

首先,我们定义了在创建线程时需要由它调用的一个函数的原型。如下所示:

void *thread_function(void *arg)

根据pthread_create的要求,它只有一个指向void的指针作为参数,返回的也是指向void的指针。稍后,我们将介绍这个函数的定义(及其实现)。

main函数中,我们首先定义了几个变量,然后调用pthread_create开始运行新线程。如下所示:

pthread_t a_thread;

void *thread_result;

res = pthread_create(&a_thread,NULL,thread_function,(void *)message);

我们向pthread_create函数传递了一个pthread_t类型对象的地址,今后可以用它来引用这个新线程。我们不想改变默认的线程的属性,所以设置第二个参数为NULL。最后两个参数分别为将要调用的函数和一个传递给该函数的参数。

如果这个调用成功了,就会有两个线程在运行。原先的线程(main)继续执行pthread_create后面的代码,而新线程开始执行thread_function函数。

原先的线程在查明新线程已经启动后,将调用pthread_join函数,如下所示:

res = pthread_join(a_thread,&thread_result);

我们给该函数传递两个参数,一个是正在等待其结束的线程的标识符,另一个是指向线程返回值的指针。这个函数将等到它所指定的线程终止后才返回。然后主线程将打印新线程的返回值和全局变量message的值,最后退出。

新线程在thread_function函数中开始执行,它先打印出自己的参数,休眠一会儿,然后更新全局变量,最后退出并向主线程返回一个字符串。新线程修改了数组message,而原先的线程也可以访问该数组。如果我们调用的是fork而不是pthread_create,就不会有这样的效果。


点击(此处)折叠或打开

  1. #include<stdio.h>
  2. #include<unistd.h>
  3. #include<stdlib.h>
  4. #include<pthread.h>
  5. #include<string.h>
  6. int run_now=1;
  7. int count=0;
  8. void *thread1_function(void *arg);
  9. void *thread2_function(void *arg);
  10. int main()
  11. {
  12.     pthread_t thread1,thread2;
  13.     int res1,res2;
  14.     void *thread1_result;
  15.     void *thread2_result;
  16.     printf("main thread is starting \n");
  17.     res1=pthread_create(&thread1,NULL,thread1_function,NULL);
  18.     if(res1!=0)
  19.     {
  20.         perror("create thread1 failed!\n");
  21.         exit(EXIT_FAILURE);
  22.     }
  23.     res2=pthread_create(&thread2,NULL,thread2_function,NULL);
  24.     if(res2!=0)
  25.     {
  26.         perror("thread2 create failed\n");
  27.         exit(EXIT_FAILURE);
  28.     }
  29.     printf("waiting for thread to finish \n");
  30.     res1=pthread_join(thread1,&thread1_result);
  31.     printf("thread1 exit returned %s\n",(char *)thread1_result);
  32.     res2=pthread_join(thread2,&thread2_result);
  33.     printf("thread2 exit returned %s\n",(char *)thread2_result);
  34.     return 0;
  35. }
  36. void *thread1_function(void *arg)
  37. {
  38.     while(count++<20)
  39.     {
  40.             if(run_now==1)
  41.             {
  42.                 printf("in thread1 count is %d\n",count);
  43.                 run_now=2;
  44.             }
  45.             else
  46.             {
  47.                 sleep(3);
  48.             }
  49.     }
  50.     pthread_exit("thread1 exit");
  51. }
  52. void *thread2_function(void *arg)
  53. {
  54.         while(count++<20)
  55.         {
  56.             if(run_now==2)
  57.             {
  58.                 printf("in thread2 count is %d\n",count);
  59.                 run_now=1;
  60.             }
  61.             else
  62.             {
  63.                 sleep(3);
  64.             }
  65.         }
  66.         pthread_exit("thread2 exit");
  67. }

每个线程通过设置run_now变量的方法来通知另一个线程开始运行,然后,它会等待另一个线程改变了这个变量的值后再次运行。这个例子显示了两个线程之间自动地交替执行,同时也再次阐明了一个观点,即这两个线程共享run_now变量。

好,这篇就介绍这这两个吧,下篇我们继续!

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