Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/c/61.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 多线程Linux程序未提供预期输出_C_Linux_Multithreading_Printf - Fatal编程技术网

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”)for
showMsg
clock

main\u pth
etc是
pthread\u create
的错误返回,而不是线程id。Wait(“join”)for
showMsg
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