C 多线程Linux程序未提供预期输出
这是我的代码,我用:C 多线程Linux程序未提供预期输出,c,linux,multithreading,printf,C,Linux,Multithreading,Printf,这是我的代码,我用: gcc thread.c -lpthread 它没有打印任何错误或警告。但是当我运行它时,程序不会打印任何东西 平台:Ubuntu 11.10 64位gcc 4.6.1 退出状态:0 当我调试它时,我发现它像我预期的那样打印hello 这是我的代码: #include <pthread.h> #include <stdio.h> #include <stdlib.h> void *loopPrint(void *ptr) {
gcc thread.c -lpthread
它没有打印任何错误或警告。但是当我运行它时,程序不会打印任何东西
平台:Ubuntu 11.10 64位gcc 4.6.1退出状态:0 当我调试它时,我发现它像我预期的那样打印
hello
这是我的代码:
#include <pthread.h>
#include <stdio.h>
#include <stdlib.h>
void *loopPrint(void *ptr)
{
char *p = (char *)ptr;
while (1)
{
printf("%s\n", p);
}
}
void *pClock(void *ptr)
{
sleep(3);
exit(0);
}
int main()
{
pthread_t showMsg, clock;
int main_pth, wait_pth;
char *msg = "Hello";
main_pth = pthread_create(&showMsg, NULL, loopPrint, (void *)msg);
wait_pth = pthread_create(&clock, NULL, pClock, NULL);
pthread_join(main_pth, NULL);
pthread_join(wait_pth, NULL);
return 0;
}
#包括
#包括
#包括
void*loopPrint(void*ptr)
{
char*p=(char*)ptr;
而(1)
{
printf(“%s\n”,p);
}
}
void*pClock(void*ptr)
{
睡眠(3);
出口(0);
}
int main()
{
pthread_t showMsg,时钟;
int main_pth,等待_pth;
char*msg=“你好”;
main_pth=pthread_create(&showMsg,NULL,loopPrint,(void*)消息);
wait_pth=pthread_create(&clock,NULL,pClock,NULL);
pthread_join(main_pth,NULL);
pthread_join(wait_pth,NULL);
返回0;
}
main\u pth
etc是pthread\u create
的错误返回,而不是线程id。Wait(“join”)forshowMsg
和clockmain\u pth
etc是pthread\u create
的错误返回,而不是线程id。Wait(“join”)forshowMsg
和clock
pthread_join(main_pth, NULL);
这是错误的,pthread\u join
将pthread\u t
作为参数。替换为:
pthread_join(showMsg, NULL);
(另一个也一样。)
并检查这些调用的返回值,它们可以也确实失败。
顺便说一句,您缺少睡眠通话的#include
这是错误的,pthread\u join
将pthread\u t
作为参数。替换为:
pthread_join(showMsg, NULL);
(另一个也一样。)
并检查这些调用的返回值,它们可以也确实失败。
顺便说一句,您缺少睡眠通话的#include