[英]Multi-threaded Programming C
我必須手動執行此代碼並計算輸出,我嘗試了一下,但找不到遺漏的內容,這是代碼:
#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 :)
為什么代碼跳過了最后一次打印?
線程中的exit
調用將在進程有機會輸出“再見”消息之前終止進程。 如果要終止線程,請使用pthread_exit
,而不是exit
。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.