Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/multithreading/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C 带pthread的远程函数_C_Multithreading_Pthreads - Fatal编程技术网

C 带pthread的远程函数

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_

我使用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_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;
}
顺便说一下:

  • 不需要
    malloc
    ing;您只需在堆栈上创建
    线程
  • 如果
    main
    函数结束时没有错误,则应从该函数返回
    0

如果不显式返回0,我会感觉更好,相反,这里有EXIT\u SUCCESS.True。并不是说它的价值永远不会改变,而是它更明确一点。谢谢。我的问题不同:当所有函数都在一个文件中时,它工作得很好。将display_prompt()移动到其他文件时,它不起作用。我添加了线程\u join,但是现在在同一个文件中显示\u prompt()不起作用。在Eclipse中有没有特殊的调试方法?
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;
}