0

I have a program that starts a pthread and later on waits for the termination of this thread before it returns. 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. So the join works and t is finished. 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.

However, if I remove the line with the pthread_join call the program exits flawlessly.

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 }. Nevertheless I have no idea what is going on behind the scenes:

Program received signal SIGSEGV, Segmentation fault.
0x000000060003aa10 in ?? ()
Bastian
  • 4,638
  • 6
  • 36
  • 55
  • I was checking out the man page and came across this: If multiple threads simultaneously try to join with the same thread, the results are undefined. If the thread calling pthread_join() is canceled, then the target thread will remain joinable (i.e., it will not be detached). Any chance you are doing something like that? – Robert Ekendahl Jun 20 '14 at 13:37
  • @RobertEkendahl But would that not be reflected in the message of `printf`? Either way I have only one main thread which creates and joins other threads. – Bastian Jun 20 '14 at 13:41
  • I don't see any obvious reason for why you would get a segfault. Can you post some more of the code? – Robert Ekendahl Jun 20 '14 at 13:44
  • @RobertEkendahl I'll try to reduce the code to the reduce the code to the bare minimum and will post it soon. – Bastian Jun 20 '14 at 13:49
  • 2
    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. Then return 0 performs an exit_process call. If in your case the stack was corrupted, it's possible the pointer to exit_process was replaced with an invalid pointer. – George Nechifor Jun 20 '14 at 14:13
  • I think that might have been the problem! I used GMP for arbitrary precision arithmetic and forgot to initialize a GMP variable before using it for calculations. – Bastian Jun 20 '14 at 16:41
  • The standard checks: Does the code compile without warnings if compiled with all warnings on `-Wall -Wextra -pedantic`. Does it run without errors under a memory checker like Valgrind (http://valgrind.com)? – alk Jun 20 '14 at 17:44
  • @GeoAoe If you post your comment as an answer I'll accept it because it was the ultimate reason of my problem (as commented before). – Bastian Jun 21 '14 at 14:26

1 Answers1

0

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. Then return 0 performs an exit_process call. If in your case the stack was corrupted, it's possible the pointer to exit_process was replaced with an invalid pointer.

George Nechifor
  • 439
  • 3
  • 12