码迷,mamicode.com
首页 > 编程语言 > 详细

线程的控制与分离

时间:2016-04-17 18:03:36      阅读:212      评论:0      收藏:0      [点我收藏+]

标签:线程的控制与分离

              线程的控制与分离

线程与进程线程可以说是系统的一个执行流,它是操作系统用于调度去cpu中执行的基

        本模块,线程它是在进程中存在的,进程相当于承担系统资源的一个实体,

        而线程是用来去执行的,它和进程

        有些是共享的:

        1.文件描述符表

        2.每种信号的处理方式(SIG_IGN、SIG_DFL或者自定义的信号处理函数)

        3.当前工作目录

        4.用户id和组id

        有些是独立的:

        1.栈空间

      2.硬件上下文(包括各种寄存器的值,栈指针,程序计数器)

        3.线程id

        4.信号屏蔽字

        5.errno的值

        6.调度优先级

线程的控制:

在posix标准下定义了以下几个函数用于线程的控制

**编译的时候要在后面包含phread这个包

       #include <pthread.h>

       int pthread_create(pthread_t *thread, const pthread_attr_t *attr,
                          void *(*start_routine) (void *), void *arg);

pthread_t *thread是一个输出型参数,用来返回一个线程号

start_routine是线程的函数,arg是传递值

pthread_arrt_t是用来设置线程的属性,我们用系统默认的NULL

该函数成功返回0;失败返回错误码

  int pthread_join(pthread_t thread, void **retval);

类似于waitpid,这个函数是用来等待线程的,输出型参数retval用来输出线程的退出码

该函数成功返回0;失败返回错误码


线程的三种返回方式:

1,从线程内部return;

2,使用函数pthread_exit函数返回;

3,在外部用pthread_cancel函数取消该进程;

下面是测试代码

  1 #include<stdio.h>
  2 #include<pthread.h>
  3 void *write1(void*arg)
  4 {
  5     int count=15;
  6     while(count-->0)
  7     {
  8         printf("write thread1\n");
  9         sleep(1);
 10     }
 11     return (void*)1;
 12 }
 13 void *write2(void*arg)
 14 {
 15     int count=18;
 16     while(count-->0)
 17     {
 18         printf("write thread2\n");
 19         sleep(1);
 20     }
 21     pthread_exit((void*)2);
 22 
 23 }
 24 void *write3(void*arg)
 25 {
 26     int count=20;
 27     while(count-->0)
 28     {
 29         printf("write thread3\n");
 30         sleep(1);
 31     }
 32 }
 33 
 34 int main()
 35 {
 36     pthread_t thread1=0;
 37     pthread_t thread2=0;
 38     pthread_t thread3=0;
 39     int ret=pthread_create(&thread1,NULL,write1,NULL);
 40     ret=pthread_create(&thread2,NULL,write2,NULL);
 41     ret=pthread_create(&thread3,NULL,write3,NULL);
 42 
 43     int count=10;
 44     while(count-->0)
 45     {
 46         printf("main thread\n");
 47         sleep(1);
 48     }
 49     void *thread_wait=0;
 50     ret=pthread_join(thread1,&thread_wait);
 51     if(ret==0)
 52     {
 53         printf("write thread1 wait%u success: %d\n",(unsigned long)thread1,(int)thread_wait);
 54 
 55     }
 56     else
 55     }
 56     else
 57     {
 58         printf("write error%s\n",strerror(ret));
 59     }
 60     ret=pthread_join(thread2,&thread_wait);
 61     if(ret==0)
 62     {
 63         printf("write thread2 wait%u success: %d\n",(unsigned long)thread2,(int)thread_wait);
 64 
 65     }
 66     else
 67     {
 68         printf("write erroe%s\n",strerror(ret));
 69     }
 70     pthread_cancel(thread3);
 71     ret=pthread_join(thread3,&thread_wait);
 72     if(ret==0)
 73     {
 74         printf("write thread3 wait%u success: %d\n",(unsigned long)thread3,(int)thread_wait);
 75 
 76     }
 77     else
 78     {
 79         printf("write erroe%s\n",strerror(ret));
 80     }
 81     return 0;
 82 }

执行结果如下

技术分享

线程的分离

因为创建完线程就要去等待它,为了让创建出来的线程自生自灭,我们可以用线程分离。

默认下创建的线程均是可结合的,而使用pthread_detach可分离线程;

分离后主进程不用再等待它,它会自动释放所有资源,使得主进程可以继续运行下面的程序

  3 void * pthread1(void *arg)
  4 {
  5     pthread_detach(pthread_self());
  6     int count=5000;
  7     while(count-->0){
  8         printf("hello\n");
  9         sleep(1);
 10     }   
 11     printf("hello\n");
 12 }   
 13 int main()
 14 {
 15     pthread_t thread1=0;
 16     int ret=pthread_create(&thread1,NULL,pthread1,NULL);
 17     int count=5;
 18     while(count-->0){
 19         printf("main hello\n");
 20         sleep(1);
 21     }   
 22     void *thread_id=0;
 23     ret=pthread_join(thread1,&thread_id);
 24     printf("%d\n",thread_id);
 25     printf("11111\n");
 26     sleep(1);
 27     return 0;
 28 }   
~                                                                                                               
~                                                                                                               
"thread_detach.c" 28L, 488C 已写入                                                            7,17-20      底端


本文出自 “痕迹” 博客,请务必保留此出处http://wpfbcr.blog.51cto.com/10696766/1764723

线程的控制与分离

标签:线程的控制与分离

原文地址:http://wpfbcr.blog.51cto.com/10696766/1764723

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!