如果进程中任一线程调用exit, _Exit, _exit, 整个进程终止. 这不是线程终止, 而是进程, 这将终止该进程中的所有线程.
1. 线程终止的方式:
单个线程可以通过3种方式退出:
- 从启动例程中返回, 返回值是线程的退出码.
- 被同一进程中的其他线程取消.
- 调用pthread_exit.
2. pthread_exit函数:
- 原型: void pthread_exit(void *rval_ptr);
- 头文件: <pthread.h>
- 参数: rval_ptr是一个无类型指针, 指向线程的返回值存储变量.
3. pthread_join函数:
- 原型: int pthread_join(pthread_t thread, void **rval_ptr);
- 头文件: <pthread.h>
- 返回值: 成功则返回0, 否则返回错误编号.
- 参数:
- thread: 线程ID.
- rval_ptr: 指向返回值的指针(返回值也是个指针).
- 说明:
- 调用线程将一直阻塞, 直到指定的线程调用pthread_exit, 从启动例程返回或被取消.
- 如果线程从它的启动例程返回, rval_ptr包含返回码.
- 如果线程被取消, 由rval_ptr指定的内存单元置为: PTHREAD_CANCELED.
- 如果对返回值不关心, 可把rval_ptr设为NULL.
4. 实例:
main.c 代码:
#include
<
pthread.h
>
#include < stdio.h >
/**/ /* print process and thread IDs */
void printids( const char * s)
... {
pid_t pid, ppid;
pthread_t tid;
pid = getpid();
ppid = getppid();
tid = pthread_self();
printf("%16s pid %5u ppid %5u tid %16u (0x%x) ",
s, (unsigned int)pid, (unsigned int)ppid,
(unsigned int)tid, (unsigned int)tid);
}
/**/ /* thread process */
void * thread_func( void * arg);
... {
printids("new thread: ");
return (void *)108;
}
/**/ /* main func */
int main()
... {
int err;
void *tret; /**//* thread return value */
pthread_t ntid;
err = pthread_create(&ntid, NULL, thread_func, NULL);
if (err != 0)
perror("can't create thread");
err = pthread_join(ntid, &tret);
if (err != 0)
perror("can't join thread");
printids("main thread: ");
printf("thread exit code: %d ", (int)tret);
sleep(1);
return 0;
}
#include < stdio.h >
/**/ /* print process and thread IDs */
void printids( const char * s)
... {
pid_t pid, ppid;
pthread_t tid;
pid = getpid();
ppid = getppid();
tid = pthread_self();
printf("%16s pid %5u ppid %5u tid %16u (0x%x) ",
s, (unsigned int)pid, (unsigned int)ppid,
(unsigned int)tid, (unsigned int)tid);
}
/**/ /* thread process */
void * thread_func( void * arg);
... {
printids("new thread: ");
return (void *)108;
}
/**/ /* main func */
int main()
... {
int err;
void *tret; /**//* thread return value */
pthread_t ntid;
err = pthread_create(&ntid, NULL, thread_func, NULL);
if (err != 0)
perror("can't create thread");
err = pthread_join(ntid, &tret);
if (err != 0)
perror("can't join thread");
printids("main thread: ");
printf("thread exit code: %d ", (int)tret);
sleep(1);
return 0;
}
这段代码是通过前一个实例改编的, 执行流程如下:
- 首先创建一个新线程, 该线程在打印完IDs后, 返回108.
- 然后用pthread_join获取该线程返回值, 存于tret中.
- 主线程打印IDs.
- 主线程打印tret, 即新线程的返回值.
5. pthread_cancel函数:
- 原型: int pthread_cancel(pthread_t tid);
- 头文件: <pthread.h>
- 返回值: 成功则返回0, 否则返回错误编号.
- 说明:
- 等同于调用了参数为PTHREAD_CANCELED的pthread_exit.
- cancel并不等待线程终止, 它仅仅是提出请求.
- 线程可以选择忽略取消方式或是控制取消方式.