继续回到解决程序
#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("hello, world! I'm son\n");
}
int main(void)
{
int error;
pthread_t son;
error

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

被折叠的 条评论
为什么被折叠?



