一、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)源代码
-
/*DATE: 2015-3-25
-
*AUTHOR: WJ
-
*DESCRIPTION: 验证几种线程到退出方式
-
*/
-
-
#include "apue.h"
-
-
void *thread_fun(void *arg)
-
{
-
//如果传到参数1,那么就采用return方式退出
-
if(strcmp("1",(char *)arg)==0)
-
{
-
printf("new thread return!\n");
-
return (void *)1;
-
}
-
//如果传入参数2,那么就采用pthreade_xit方式退出
-
if(strcmp("2",(char *)arg)==0)
-
{
-
printf("new thread pthread_exit!\n");
-
pthread_exit((void *)2);
-
}
-
//如果传入参数3,那么就采用exit方式退出
-
if(strcmp("3",(char *)arg)==0)
-
{
-
printf("new thread exit!\n");
-
exit(3);
-
}
-
}
-
-
int main(int argc, char *argv[])
-
{
-
int err;
-
pthread_t tid;
-
-
err = pthread_create(&tid, NULL, thread_fun, (void *)argv[1]);
-
if(err != 0)
-
{
-
printf("create new thread failed\n");
-
return 0;
-
}
-
-
sleep(1);
-
printf("main thread\n");
-
return 0;
-
}
2、证明任意一个线程调用exit函数都会导致进程退出
-
/*DATE: 2015-3-25
-
*AUTHOR: WJ
-
*DESCRIPTION: 创建10 个线程,如果线程的参数和随机数相同,那么线程就采用exit方式退出
-
*/
-
-
#include "apue.h"
-
-
int num;
-
-
void *thread_fun(void *arg)
-
{
-
printf("I'm new thread: %d\n", (int *)arg);
-
-
//如果线程到参数arg和产生到随机数相同,那么线程就采用exit方式退出
-
if(num == (int *)arg)
-
{
-
printf("new thread %d exit!!!\n", (int *)arg);
-
exit(0);
-
}
-
//否则线程睡眠,让其他线程执行
-
sleep(2);
-
-
pthread_exit((void *)0);
-
}
-
-
int main()
-
{
-
int err;
-
pthread_t tid;
-
-
//产生随机种子
-
srand((unsigned int)time(NULL));
-
//产生一个10以内到随机数
-
num = rand()%11;
-
-
int i=10;
-
//创建10个线程,给每个线程传递参数i
-
while(i--)
-
{
-
err = pthread_create(&tid, NULL, thread_fun, (void *)i);
-
if(err != 0)
-
{
-
printf("create new thread failed\n");
-
return 0;
-
}
-
}
-
//睡眠1s,让新线程先运行
-
sleep(1);
-
return 0;
-
}
阅读(17278) | 评论(2) | 转发(3) |