2.pthread_join()、pthread_exit()、pthread_cancel()简述

本文简述了在多线程编程中,pthread_join(), pthread_exit()和pthread_cancel()三个函数的作用。pthread_join()用于等待线程结束并可获取退出状态,通常value_ptr参数设为NULL。pthread_exit()终止当前线程并可传递返回值,而pthread_cancel()则请求取消指定线程,不阻塞调用线程,成功返回0,失败返回非零错误码。" 124571818,9138460,Unity3D虚拟现实实验:卡牌游戏开发与优化,"['虚拟现实引擎', 'Unity', '游戏引擎', '实验', '课程设计']

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

继续回到解决程序

#include <stdio.h>
#include <stdlib.h>
#include <pthread.h>
void *sayhello(void *arg)
{
printf("hello, world! I'm son\n");
}
int main(void)
{
int error;
pthread_t son;
error = pthread_create(&son, NULL, sayhello, NULL);
printf("hello, world! I'm father\n");
return 0;
}
的问题上!

也即让“printf("hello, world! I'm son\n");”和“printf("hello, world! I'm father\n");”都能够执行。

《pthread_create()初体验》已经给出了三个解决办法:

方法1:在“return 0;”之前加上一句“sleep(1);”

方法2:在“父进程”所对应程序中加上一句“pthread_join()”

方法3:在“父进程”所对应程序中加上一句“pthread_exit(NULL)”

pthread_exit(NULL)和pthread_join()的功能差不多,只是后者从退出的子线程获取退出状态码(具体如何获取就不知道了)!

#include <stdio.h>
#include <stdlib.h>
#include <pthread.h>
void *sayhello(void *args)
{
printf
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值