Joshua Leung
Joshua Leung

Reputation: 2399

Why pthread_join on itself doesn't result in error or deadlock?

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?

Upvotes: 0

Views: 198

Answers (1)

RamV13
RamV13

Reputation: 567

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

Upvotes: 3

Related Questions