简体   繁体   English

成功pthread_join后main()不会终止

[英]main() does not terminate after successful pthread_join

I have a program that starts a pthread and later on waits for the termination of this thread before it returns. 我有一个启动pthread的程序,以后再等待该线程的终止再返回。 The code is something like: 代码类似于:

int main(int32_t argc, char* argv[]) {
  pthread_t t;
  /* initialization and other stuff 
     ... 
  */
  printf("join result:%d\n", pthread_join(t, 0));
  return 0;
}

The program prints, as it is supposed to: join result: 0 . 程序将按预期方式进行打印: join result: 0 So the join works and t is finished. 这样,联接起作用并且t完成。 Nevertheless the program does not stop execution. 但是,程序不会停止执行。 I can only force it to stop if I insert a command exit(0) (or some other number) before the return 0 line. 只有在return 0行之前插入命令exit(0) (或其他一些数字)时,我才能强制其停止。

However , if I remove the line with the pthread_join call the program exits flawlessly. 但是 ,如果我删除了带有pthread_join调用的行,程序将完美退出。

How is this even possible? 这怎么可能? What could keep a program from finishing execution after all sub-threads are joined? 所有子线程都加入后,什么可以阻止程序完成执行?

EDIT: I just found out that gdb tells me I get a segmentation fault after the execution of the last line with } . 编辑:我刚刚发现gdb告诉我使用}执行最后一行后,我遇到了段错误。 Nevertheless I have no idea what is going on behind the scenes: 尽管如此,我不知道幕后发生了什么:

Program received signal SIGSEGV, Segmentation fault.
0x000000060003aa10 in ?? ()

I think it may be possible that a stack corruption occurs in the main thread. 我认为主线程中可能发生堆栈损坏 From windows i know that before executing main, the adress of the exit_process function is pushed onto the stack. 从Windows我知道在执行main之前,exit_process函数的地址会被压入堆栈。 Then return 0 performs an exit_process call. 然后返回0执行exit_process调用。 If in your case the stack was corrupted, it's possible the pointer to exit_process was replaced with an invalid pointer. 如果在您的情况下,堆栈已损坏,则有可能将指向exit_process的指针替换为无效的指针。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM