Chinaunix首页 | 论坛 | 博客
  • 博客访问: 4258230
  • 博文数量: 1148
  • 博客积分: 25453
  • 博客等级: 上将
  • 技术积分: 11949
  • 用 户 组: 普通用户
  • 注册时间: 2010-05-06 21:14
文章分类

全部博文(1148)

文章存档

2012年(15)

2011年(1078)

2010年(58)

分类: C/C++

2011-05-14 15:53:28

#include   包含头文件

pthread_create:


在 pthread_create 的第三个参数,是这么定义的:
    void *(* _start_routine) (void *)


1. 在第一个程序中,我们定义 子线程运行 函数地址
   void
*hello(struct message *str)
   在 pthread_create中调用方式为:
   pthread_create(&thread_id, NULL,
(void *)*hello, &test);

或者是
    void *a;
    a=*hello;
    pthread_create(&thread_id,NULL,(void *)a,&test);
 
    

2. 在第二个程序中,我们定义 子线程运行 函数
   void
thread(void)
   在 pthread_create 中调用方式为:
 ret=pthread_create(&id,NULL,
(void *) thread,NULL);
    thread 本身就是表示 函数的地址了
    (void *) thread 还是转换为地址


关于 *hello() 和 hello() 的解释看下一篇
  1. #include <pthread.h> //pthread_create
  2. #include <stdio.h> //printf

  3. struct message
  4. {
  5.     int i;
  6.     int j;
  7. };

  8. void *hello(struct message *str)   带 *
  9. {
  10.     printf("the arg.i is %d\n",str->i);
  11.     printf("the arg.j is %d\n",str->j);
  12. }

  13. int main(int argc, char *argv[])
  14. {
  15.     struct message test;
  16.     pthread_t thread_id; //typedef unsigned long int pthread_t
  17.     test.i=10;
  18.     test.j=20;
  19.     
  20.     void *a;
  21.     a=*hello;   //两种方法都可以实现 ,注释掉代码的也可以实现
  22.     //pthread_create(&thread_id, NULL, (void *)*hello, &test);
  23.     pthread_create(&thread_id, NULL, (void *)a, &test);
  24.      printf("the new thread id is %u\n",thread_id);
  25.      pthread_join(thread_id,NULL); //在主线程等待子线程结束
  26. }
  27. /* void *(*__start_routine) (void *);
  28.    void *(* hello)
  29. */


  1. ywx@yuweixian:~/yu/professional/4$ gcc pthread_create.c -lpthread -o pthread_create

  1. ywx@yuweixian:~/yu/professional/4$ ./pthread_create
  2. the arg.i is 10                 子线程中执行
  3. the arg.j is 20
  4. the new thread id is 3078941552    在main 函数中
  5. ywx@yuweixian:~/yu/professional/4$ ./pthread_create
  6. the new thread id is 3078290288   在 main 函数中执行
  7. the arg.i is 10                 在子线程中执行
  8. the arg.j is 20

    我运行了两次 pthread_create程序,我们发现 这两次运行的结果不相同,这说明了两个线程争夺CPU资源的结果


  1. #include <stdio.h>
  2. #include <pthread.h>

  3. void thread(void) 没有 *
  4. {
  5.      int i;
  6.     for(i=0;i<3;i++)
  7.     printf("%d This is a pthread\n",i);
  8. }

  9. int main(void)
  10. {
  11.     pthread_t id;
  12.     int i,ret;
  13.     ret=pthread_create(&id,NULL,(void *) thread,NULL);
  14.     if(ret!=0)
  15.     {
  16.         printf ("Create pthread error!n");
  17.         exit (1);
  18.     }
  19.     for(i=0;i<3;i++)
  20.     printf("%d This is the main process.\n",i);
  21.     
  22.     pthread_join(id,NULL);

  23.     return (0);
  24. }

运行结果不一样,说明两个线程在争夺CPU的过程

  1. ywx@yuweixian:~/yu/professional/4$ ./pthread_create 0 This is the main process.
  2. 1 This is the main process.
  3. 2 This is the main process.
  4. 0 This is a pthread
  5. 1 This is a pthread
  6. 2 This is a pthread
  7. ywx@yuweixian:~/yu/professional/4$ ./pthread_create
  8. 0 This is the main process.
  9. 1 This is the main process.
  10. 0 This is a pthread
  11. 1 This is a pthread
  12. 2 This is a pthread
  13. 2 This is the main process.









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