我必须手动执行这段代码并计算出输出,我尝试了一下,但找不到遗漏的内容,代码如下:
#include <stdio.h>
#include <stdlib.h>
#include <pthread.h>
void *thread_function(void *arg) {
printf("Hello World :) \n");
exit(EXIT_SUCCESS);
}
int main(void) {
pthread_t mythread;
if(pthread_create(&mythread, NULL, thread_function, NULL)) {
fprintf(stderr, "Failure 1?\n");
exit(EXIT_FAILURE);
}
printf("I have to wait ? \n");
if (pthread_join(mythread, NULL)){
fprintf(stderr, "Failure 2 ?");
exit(EXIT_FAILURE);
}
printf("Goodbye Cruel World :(\n");
return 0;
}预期输出为:
I have to wait ?
Hello World :)
Goodbye Cruel World :(我得到的输出是:
I have to wait ?
Hello World :)为什么代码跳过了最后一次打印?
发布于 2020-03-09 05:33:18
线程中对exit的调用将在进程有机会输出“再见”消息之前终止该进程。如果您想要终止线程,请使用pthread_exit,而不是exit。
https://stackoverflow.com/questions/60590104
复制相似问题