【问题标题】:Why in this case the pid of the calling process and the new thread are the same?为什么在这种情况下调用进程和新线程的pid相同?
【发布时间】: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 应该是不同的,无论你在主线程还是其他线程中得到它?

标签: c++ process


猜你喜欢
  • 1970-01-01
  • 2012-03-22
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2021-08-09
  • 2019-12-27
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多