【发布时间】:2022-06-17 04:49:12
【问题描述】:
//用-pthread编译链接
#include
#include
#include
void *pthread_function( void *ptr ){
printf("Hello from the new thread, my pid is %d\n", getpid());
}
int main() {
printf("Hello from the calling process, my pid is %d\n", getpid());
pthread_t thread;
int ret;
ret = pthread_create( &thread, NULL, pthread_function, NULL);
if (ret){
printf("ERROR; return code from pthread_create() is %d\n", ret);
exit(-1);
}
/* Last thing that main() should do */
pthread_exit(NULL);
}
当我运行这个程序时,我得到了这个结果
来自调用进程的您好,我的 pid 是 4445
来自新线程的您好,我的 pid 是 4445
为什么这种情况下调用进程和新线程的pid是一样的?
【问题讨论】:
-
为什么 pid 应该是不同的,无论你在主线程还是其他线程中得到它?