linux如何等待子线程退出

2025-11-24 11:25:41

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如何等待子线程退出

声明:本网站引用、摘录或转载内容仅供网站访问者交流或参考,不代表本站立场,如存在版权或非法内容,请联系站长删除,联系邮箱:site.kefu@qq.com。
猜你喜欢