小编abo*_*ria的帖子

为什么加入一个线程最终会调用多个线程?

我正在学习有关线程和Linux上C语言中的线程编程的知识。我了解的是,加入线程只是调用线程并等待其执行,就像等待子进程运行一样。但是不知道为什么在尝试加入一个线程时最终会调用两个线程!

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

void *start1()
{
    printf("Hello from Thread 1\n");

}

void *start2()
{
    printf("Hello from Thread 2\n");
}

void main()
{
    pthread_t t1,t2;

    pthread_create(&t1,NULL,start1,NULL);
    pthread_create(&t2,NULL,start2,NULL);
    pthread_join(t1,NULL); 
}
Run Code Online (Sandbox Code Playgroud)

当我运行代码时,输​​出如下:

[root@localhost]# ./a.out 
Hello from Thread 1
Hello from Thread 2
Run Code Online (Sandbox Code Playgroud)

我希望它仅调用start1的代码。

multithreading pthreads process

0
推荐指数
1
解决办法
55
查看次数

标签 统计

multithreading ×1

process ×1

pthreads ×1