Chinaunix首页 | 论坛 | 博客
  • 博客访问: 5714009
  • 博文数量: 409
  • 博客积分: 0
  • 博客等级: 民兵
  • 技术积分: 8273
  • 用 户 组: 普通用户
  • 注册时间: 2013-10-23 19:15
个人简介

qq:78080458 学习交流群:150633458

文章分类

全部博文(409)

文章存档

2019年(127)

2018年(130)

2016年(20)

2015年(60)

2014年(41)

2013年(31)

分类: 嵌入式

2015-04-10 09:34:36

一、exit是危险的
如果进程中的任意一个线程调用了exit,_Exit,_exit,那么整个进程就会终止

二、不终止进程的退出方式
普通的单个线程有一下3中方式退出,这样不会终止进程
(1)从启动例程中返回,返回值是线程的退出码
(2)线程可以被同一进程中的其他线程取消
(3)线程调用pthread_exit(void *rval)函数,rval是退出码

void pthread_exit(void *rval)
rval是个无类型的指针,保存线程的退出码,其他线程可以通过返回码链接这个线程

三、手册
PTHREAD_EXIT(3)            Linux Programmer’s Manual           PTHREAD_EXIT(3)

NAME
       pthread_exit - terminate calling thread

SYNOPSIS
       #include

       void pthread_exit(void *retval);

       Compile and link with -pthread.
        //连接线程库
DESCRIPTION
       The  pthread_exit()  function  terminates the calling thread and returns a value via retval that (if the thread is join-
       able) is available to another thread in the same process that calls pthread_join(3).
        //pthread_exit(void *retval)会导致调用它的线程结束,而且返回一个值保存在retval,其他线程可以通过retval来连接到
        //该线程


       Any clean-up handlers established by pthread_cleanup_push(3) that have not yet been popped, are popped (in  the  reverse
       of  the  order  in  which  they  were pushed) and executed.  If the thread has any thread-specific data, then, after the
       clean-up handlers have been executed, the corresponding destructor functions are called, in an unspecified order.
        //通过pthread_cleanup_push(3)建立的清理函数如果还没有弹出执行,那么pthread_exit(void *retval)会导致该函数弹出执行

       When a thread terminates, process-shared resources (e.g., mutexes, condition variables, semaphores,  and  file  descrip-
       tors) are not released, and functions registered using atexit(3) are not called.
        //当一个线程结束了,共享的进程资源并没有被释放

       After the last thread in a process terminates, the process terminates as by calling exit(3) with an exit status of zero;
       thus, process-shared resources are released and functions registered using atexit(3) are called.


RETURN VALUE
       This function does not return to the caller.
        //这个函数没有返回值
ERRORS
       This function always succeeds.
        //这个函数总是成功

CONFORMING TO
       POSIX.1-2001.

NOTES
       Performing a return from the start function of any thread other than the main thread results  in  an  implicit  call  to
       pthread_exit(), using the function’s return value as the thread’s exit status.

       To  allow  other  threads  to continue execution, the main thread should terminate by calling pthread_exit() rather than
       exit(3).
        //为了让其他线程继续执行,主线程退出的时候应该使用pthread_exit(),而不是exit()

四、实例
1、几种退出方式
    (1)程序架构

    (2)源代码

点击(此处)折叠或打开

  1. /*DATE:            2015-3-25
  2.  *AUTHOR:        WJ
  3.  *DESCRIPTION:    验证几种线程到退出方式
  4.  */

  5. #include "apue.h"

  6. void *thread_fun(void *arg)
  7. {
  8.     //如果传到参数1,那么就采用return方式退出
  9.     if(strcmp("1",(char *)arg)==0)
  10.     {
  11.         printf("new thread return!\n");
  12.         return (void *)1;
  13.     }
  14.     //如果传入参数2,那么就采用pthreade_xit方式退出
  15.     if(strcmp("2",(char *)arg)==0)
  16.     {
  17.         printf("new thread pthread_exit!\n");
  18.         pthread_exit((void *)2);
  19.     }
  20.     //如果传入参数3,那么就采用exit方式退出
  21.     if(strcmp("3",(char *)arg)==0)
  22.     {
  23.         printf("new thread exit!\n");
  24.         exit(3);
  25.     }
  26. }

  27. int main(int argc, char *argv[])
  28. {
  29.     int err;
  30.     pthread_t tid;

  31.     err = pthread_create(&tid, NULL, thread_fun, (void *)argv[1]);
  32.     if(err != 0)
  33.     {
  34.         printf("create new thread failed\n");
  35.         return 0;
  36.     }

  37.     sleep(1);
  38.     printf("main thread\n");
  39.     return 0;
  40. }
2、证明任意一个线程调用exit函数都会导致进程退出

点击(此处)折叠或打开

  1. /*DATE:        2015-3-25
  2.  *AUTHOR:        WJ
  3.  *DESCRIPTION: 创建10 个线程,如果线程的参数和随机数相同,那么线程就采用exit方式退出
  4.  */

  5. #include "apue.h"

  6. int num;

  7. void *thread_fun(void *arg)
  8. {
  9.     printf("I'm new thread: %d\n", (int *)arg);

  10.     //如果线程到参数arg和产生到随机数相同,那么线程就采用exit方式退出
  11.     if(num == (int *)arg)
  12.     {
  13.         printf("new thread %d exit!!!\n", (int *)arg);
  14.         exit(0);
  15.     }
  16.     //否则线程睡眠,让其他线程执行
  17.     sleep(2);
  18.     
  19.     pthread_exit((void *)0);
  20. }

  21. int main()
  22. {
  23.     int err;
  24.     pthread_t tid;

  25.     //产生随机种子
  26.     srand((unsigned int)time(NULL));
  27.     //产生一个10以内到随机数
  28.     num = rand()%11;

  29.     int i=10;
  30.     //创建10个线程,给每个线程传递参数i
  31.     while(i--)
  32.     {
  33.         err = pthread_create(&tid, NULL, thread_fun, (void *)i);
  34.         if(err != 0)
  35.         {
  36.             printf("create new thread failed\n");
  37.             return 0;
  38.         }
  39.     }
  40.     //睡眠1s,让新线程先运行
  41.     sleep(1);
  42.     return 0;
  43. }



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

charon__2019-12-30 17:46:26

err = pthread_create(&tid, NULL, thread_fun, (void *)i);
还有这一句,i没有取地址。但是不取地址我能打印出来ID,取了地址,打印不出来线程ID

charon__2019-12-30 17:18:36

if(num == (int *)arg)
问一下我这句编译的时候过不去啊?