linux如何等待子线程退出

时间:2026-02-14 17:46:14

1、等待子线程退出的思路

1) 主线程中,使用pthread_create创建子线程

2) 主线程中,使用pthread_join等待子线程退出

2、pthread_create的语法形式

类库:#include <pthread.h>

原型:

int pthread_create(pthread_t * thread, const pthread_attr_t * attr, 

     void * (*start_routine)(void*)), void * arg);

参数:

thread            -> 线程ID

attr                 -> 线程属性

start_routine  -> 线程函数

arg                  -> 线程入参 

3、pthread_join的语法形式

类库:#include <pthread.h>

原型:int pthread_join(pthread_t thread, void **retval);

参数:

thread -> 线程ID

retval  -> 线程推出返回值

4、创建子线程,等待子线程退出

命令:

[root]#touch main.cpp

[root]#vi main.cpp

linux如何等待子线程退出

linux如何等待子线程退出

5、编译

备注:编译需要链接pthread库。

linux如何等待子线程退出

6、运行

备注:符合预期,测试OK!

linux如何等待子线程退出

© 2026 途途旅游
信息来自网络 所有数据仅供参考
有疑问请联系站长 site.kefu@gmail.com