小编Mic*_*key的帖子

简单的pthread代码中的奇怪结果

我写了以下代码:

#include <pthread.h>
#include <stdio.h>

void* sayHello (void *x){
    printf ("Hello, this is %d\n", (int)pthread_self());
    return NULL;
}

int main (){
    pthread_t thread;
    pthread_create (&thread, NULL, &sayHello, NULL);
    printf("HERE\n");
    return 0;
}
Run Code Online (Sandbox Code Playgroud)

编译和运行后,我看到了3种不同类型的输出.

  1. 只打印了"Here".
  2. "这里"和1'sayHello'的消息.
  3. "这里"和2'sayHello'的消息.

当然我对第二个选项没关系,但是我不明白为什么如果我只创建一个线程,'sayHello'massege可以打印0或2次?

c multithreading pthreads

3
推荐指数
1
解决办法
92
查看次数

标签 统计

c ×1

multithreading ×1

pthreads ×1