0
votes

https://repl.it/repls/ColdSilentTriangles

#include <stdio.h>
#include <pthread.h>
pthread_t th;

void *fp(void *args) {
  printf("Thread running...");
  pthread_join(th, NULL);
  printf("Thread waiting...");
  return NULL;
}

int main(void) {
  printf("Hello World\n");
  pthread_create(&th, NULL, fp, NULL);
  pthread_join(th, NULL);
  return 0;
}

From man pthread_join,

The pthread_join() function suspends execution of the calling thread until the target thread ter-minates unless the target thread has already terminated.

Why the program doesn't result in deadlock? Why there is no error generated? Why "Thread waiting..." is outputed onto the screen when the pthread_join() is supposed to block the thread?

1
How do you know? One of the possible return values of pthread_join() is EDEADLK, but you don't even check. - Iwillnotexist Idonotexist
Yeah, it's really weird that you say no error is generated when you didn't even check. - ikegami

1 Answers

3
votes

pthread_join doesn't block execution and instead returns EDEADLK in your example.