C 带pthread的远程函数
我使用pthread用c编写了一些代码(我首先在eclipseide中配置了链接器和编译器)C 带pthread的远程函数,c,multithreading,pthreads,C,Multithreading,Pthreads,我使用pthread用c编写了一些代码(我首先在eclipseide中配置了链接器和编译器) #包括 #包括“starter.h” #包括“UI.h” 第页*MM; 页面*磁盘; PCB*所有PCB阵列; void*显示提示(void*id){ printf(“Hello111\n”); 返回NULL; } int main(int argc,字符**argv){ printf(“Hello\n”); pthread_t*线程=(pthread_t*)malloc(sizeof(pthread_
#包括
#包括“starter.h”
#包括“UI.h”
第页*MM;
页面*磁盘;
PCB*所有PCB阵列;
void*显示提示(void*id){
printf(“Hello111\n”);
返回NULL;
}
int main(int argc,字符**argv){
printf(“Hello\n”);
pthread_t*线程=(pthread_t*)malloc(sizeof(pthread_t));
pthread_create(线程,NULL,显示提示,NULL);
printf(“Hello\n”);
返回1;
}
那很好。但是,当我将display_提示符移动到UI.h时
未打印“Hello111”输出
有人知道如何解决这个问题吗?
Elad当
main
返回时,所有线程终止。如果你创建的线程在那一刻没有打印任何内容,它永远不会打印。这取决于机会,而不是函数实现的位置
要让main
等待线程完成,请使用pthread\u join
:
int main(int argc, char** argv) {
printf("Hello\n");
pthread_t thread;
pthread_create(&thread, NULL, display_prompt, NULL);
printf("Hello\n");
pthread_join(thread);
return 0;
}
顺便说一下:
- 不需要
ing;您只需在堆栈上创建malloc
线程
- 如果
函数结束时没有错误,则应从该函数返回main
0
int main(int argc, char** argv) {
printf("Hello\n");
pthread_t thread;
pthread_create(&thread, NULL, display_prompt, NULL);
printf("Hello\n");
pthread_join(thread);
return 0;
}