一 结束线程
1 当线程回调函数执行完后,即在回调函数中调用return语句时,默认线程终止(自然死亡)
2 显示调用pthread_exit函数(自尽^_^)。
3 调用exit函数,结束进程,也包括进程下的所有线程(地球消失了,自然活在地球上的人也就挂掉了^_^)
二 pthread_exit与return的区别
在主线程中(以main为回调函数的线程):
1· 在main中
调用return语句,会结束整个进程,即使进程下还有其他线程在工作。
调用pthread_exit,仅仅是结束main所在的线程,如果进程下还有其他线程在工作,则进程继续存在,其他线程正常工作。
2· 在一般线程中
在一般线程的回调函数中,调用return和pthread_exit效果是一样的。
当想要在回调函数调用的方法中结束线程时只能用pthread_exit。
三 测试代码
#include <stdio.h>#include <stdlib.h>
#include <pthread.h>
void *thread1_proc_function(void *argements)
{
int nCount = 0;
while(1)
{
printf("thread1 nCount =