Linux 多线程(二)

在实验1的基础上提出两个问题

问题:1.如何给线程执行函数传入结构体参数

          2.如何获取线程执行函数的返回值

          3.线程退出和取消

pthread_join(turtle,(void **)&result);//第二个参数就会保留线程执行函数的返回值

线程退出和取消:return 语句

                         pthread_exit()函数

                         pthread_cancel()函数,被同一进程的其他线程取消

tips:exit()函数会退出整个进程

实验源码如下:

#include <pthread.h>
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
typedef struct
{
   char name[20];
   int time;
   int start;
   int end;
}RaceArg;
void * th_fn(void *arg)
{
   RaceArg *r=(RaceArg *)arg;
   for(int i=r->start;i<r->end;i++)
   {
      printf("%lx run %d, name=%s\n",pthread_self(),i,r->name);
      usleep(r->time);
   }
   //return (void*)0;
   pthread_exit((void*)(r->end-r->start));
}


int main(void)
{
   int err;
   pthread_t rabbit,turtle;
   RaceArg r_a={"rabbit",10000,2,50};
   RaceArg r_b={"turtle",10000,3,100};


   if((err=pthread_create(&rabbit,NULL,th_fn,(void *)&r_a))!=0)
   {
      perror("pthread_create error");
   }
   if((err=pthread_create(&turtle,NULL,th_fn,(void *)&r_b))!=0)
   {
      perror("pthread_create error");
   }
   int *result;
   pthread_join(rabbit,(void **)&result);
   printf("the result is %d\n",(int)result);
   pthread_join(turtle,(void **)&result);
   printf("the result is %d\n",(int)result);
   printf("control thread id: %lx\n",pthread_self());
   printf("finished!\n");
   return 0;

}


实验结果如下:

7f4ed2118700 run 3, name=turtle
7f4ed2919700 run 2, name=rabbit
7f4ed2118700 run 4, name=turtle
7f4ed2919700 run 3, name=rabbit
7f4ed2118700 run 5, name=turtle
7f4ed2919700 run 4, name=rabbit
7f4ed2118700 run 6, name=turtle
7f4ed2919700 run 5, name=rabbit
7f4ed2118700 run 7, name=turtle
7f4ed2919700 run 6, name=rabbit
7f4ed2118700 run 8, name=turtle
7f4ed2919700 run 7, name=rabbit
7f4ed2118700 run 9, name=turtle
7f4ed2919700 run 8, name=rabbit
7f4ed2118700 run 10, name=turtle
7f4ed2919700 run 9, name=rabbit
7f4ed2118700 run 11, name=turtle
7f4ed2919700 run 10, name=rabbit
7f4ed2118700 run 12, name=turtle
7f4ed2919700 run 11, name=rabbit
7f4ed2118700 run 13, name=turtle
7f4ed2919700 run 12, name=rabbit
7f4ed2118700 run 14, name=turtle
7f4ed2919700 run 13, name=rabbit
7f4ed2118700 run 15, name=turtle
7f4ed2919700 run 14, name=rabbit
7f4ed2118700 run 16, name=turtle
7f4ed2919700 run 15, name=rabbit
7f4ed2118700 run 17, name=turtle
7f4ed2919700 run 16, name=rabbit
7f4ed2118700 run 18, name=turtle
7f4ed2919700 run 17, name=rabbit
7f4ed2118700 run 19, name=turtle
7f4ed2919700 run 18, name=rabbit
7f4ed2118700 run 20, name=turtle
7f4ed2919700 run 19, name=rabbit
7f4ed2118700 run 21, name=turtle
7f4ed2919700 run 20, name=rabbit
7f4ed2118700 run 22, name=turtle
7f4ed2919700 run 21, name=rabbit
7f4ed2118700 run 23, name=turtle
7f4ed2919700 run 22, name=rabbit
7f4ed2118700 run 24, name=turtle
7f4ed2919700 run 23, name=rabbit
7f4ed2118700 run 25, name=turtle
7f4ed2919700 run 24, name=rabbit
7f4ed2118700 run 26, name=turtle
7f4ed2919700 run 25, name=rabbit
7f4ed2118700 run 27, name=turtle
7f4ed2919700 run 26, name=rabbit
7f4ed2118700 run 28, name=turtle
7f4ed2919700 run 27, name=rabbit
7f4ed2118700 run 29, name=turtle
7f4ed2919700 run 28, name=rabbit
7f4ed2118700 run 30, name=turtle
7f4ed2919700 run 29, name=rabbit
7f4ed2118700 run 31, name=turtle
7f4ed2919700 run 30, name=rabbit
7f4ed2118700 run 32, name=turtle
7f4ed2919700 run 31, name=rabbit
7f4ed2118700 run 33, name=turtle
7f4ed2919700 run 32, name=rabbit
7f4ed2118700 run 34, name=turtle
7f4ed2919700 run 33, name=rabbit
7f4ed2118700 run 35, name=turtle
7f4ed2919700 run 34, name=rabbit
7f4ed2118700 run 36, name=turtle
7f4ed2919700 run 35, name=rabbit
7f4ed2118700 run 37, name=turtle
7f4ed2919700 run 36, name=rabbit
7f4ed2118700 run 38, name=turtle
7f4ed2919700 run 37, name=rabbit
7f4ed2118700 run 39, name=turtle
7f4ed2919700 run 38, name=rabbit
7f4ed2118700 run 40, name=turtle
7f4ed2919700 run 39, name=rabbit
7f4ed2118700 run 41, name=turtle
7f4ed2919700 run 40, name=rabbit
7f4ed2118700 run 42, name=turtle
7f4ed2919700 run 41, name=rabbit
7f4ed2118700 run 43, name=turtle
7f4ed2919700 run 42, name=rabbit
7f4ed2118700 run 44, name=turtle
7f4ed2919700 run 43, name=rabbit
7f4ed2118700 run 45, name=turtle
7f4ed2919700 run 44, name=rabbit
7f4ed2118700 run 46, name=turtle
7f4ed2919700 run 45, name=rabbit
7f4ed2118700 run 47, name=turtle
7f4ed2919700 run 46, name=rabbit
7f4ed2118700 run 48, name=turtle
7f4ed2919700 run 47, name=rabbit
7f4ed2118700 run 49, name=turtle
7f4ed2919700 run 48, name=rabbit
7f4ed2118700 run 50, name=turtle
7f4ed2919700 run 49, name=rabbit
7f4ed2118700 run 51, name=turtle
the result is 48
7f4ed2118700 run 52, name=turtle
7f4ed2118700 run 53, name=turtle
7f4ed2118700 run 54, name=turtle
7f4ed2118700 run 55, name=turtle
7f4ed2118700 run 56, name=turtle
7f4ed2118700 run 57, name=turtle
7f4ed2118700 run 58, name=turtle
7f4ed2118700 run 59, name=turtle
7f4ed2118700 run 60, name=turtle
7f4ed2118700 run 61, name=turtle
7f4ed2118700 run 62, name=turtle
7f4ed2118700 run 63, name=turtle
7f4ed2118700 run 64, name=turtle
7f4ed2118700 run 65, name=turtle
7f4ed2118700 run 66, name=turtle
7f4ed2118700 run 67, name=turtle
7f4ed2118700 run 68, name=turtle
7f4ed2118700 run 69, name=turtle
7f4ed2118700 run 70, name=turtle
7f4ed2118700 run 71, name=turtle
7f4ed2118700 run 72, name=turtle
7f4ed2118700 run 73, name=turtle
7f4ed2118700 run 74, name=turtle
7f4ed2118700 run 75, name=turtle
7f4ed2118700 run 76, name=turtle
7f4ed2118700 run 77, name=turtle
7f4ed2118700 run 78, name=turtle
7f4ed2118700 run 79, name=turtle
7f4ed2118700 run 80, name=turtle
7f4ed2118700 run 81, name=turtle
7f4ed2118700 run 82, name=turtle
7f4ed2118700 run 83, name=turtle
7f4ed2118700 run 84, name=turtle
7f4ed2118700 run 85, name=turtle
7f4ed2118700 run 86, name=turtle
7f4ed2118700 run 87, name=turtle
7f4ed2118700 run 88, name=turtle
7f4ed2118700 run 89, name=turtle
7f4ed2118700 run 90, name=turtle
7f4ed2118700 run 91, name=turtle
7f4ed2118700 run 92, name=turtle
7f4ed2118700 run 93, name=turtle
7f4ed2118700 run 94, name=turtle
7f4ed2118700 run 95, name=turtle
7f4ed2118700 run 96, name=turtle
7f4ed2118700 run 97, name=turtle
7f4ed2118700 run 98, name=turtle
7f4ed2118700 run 99, name=turtle
the result is 97
control thread id: 7f4ed311c700
finished!

阅读更多
想对作者说点什么? 我来说一句

Linux多线程Linux多线程

2010年08月26日 1.27MB 下载

Linux多线程服务端编程

2018年04月28日 3.26MB 下载

没有更多推荐了,返回首页

不良信息举报

Linux 多线程(二)

最多只允许输入30个字

加入CSDN,享受更精准的内容推荐,与500万程序员共同成长!
关闭
关闭