1

I found this example; here the two threads threadDefault and threadCustomized are terminated by using pthread_exit followed by return. Why did the author write both instructions?

m0hithreddy
  • 1,752
  • 1
  • 10
  • 17
Gennaro Arguzzi
  • 769
  • 1
  • 14
  • 27

2 Answers2

2

Mohith Reddy's answer is correct but misses the point. Of course the return statement is never executed since pthread_exit doesn't return, but it's there to suppress warnings from the compiler in case it's not aware that pthread_exit doesn't return.

R.. GitHub STOP HELPING ICE
  • 208,859
  • 35
  • 376
  • 711
  • Yes that should be it! Even I am remembering now inserting a ``return`` myself after ``pthread_exit`` to suppress the compiler warnings. – m0hithreddy Jun 30 '20 at 11:46
  • Thank you very much for your answer @R...GitHubSTOPHELPINGICE Could you do an example showing the warning you're talking about please? – Gennaro Arguzzi Jun 30 '20 at 15:12
1

From the pthread_exit() man page:

RETURN VALUE

This function does not return to the caller.

ERRORS

This function always succeeds.

NOTES

Performing a return from the start function of any thread other than the main thread results in an implicit call to pthread_exit(), using the function's return value as the thread's exit status.

The above all suggests that pthread_exit(); and return NULL; are redundant when both are called together.

m0hithreddy
  • 1,752
  • 1
  • 10
  • 17
  • Hi @MohithReddy the strange thing is that, after the execution of the pthread_exit, the return should not be executed because the pthread_exit terminates the thread. – Gennaro Arguzzi Jun 30 '20 at 09:05
  • @GennaroArguzzi Yes, that is how it should happen. Is ``return`` getting called after ``pthread_exit()``? If yes, how are you making sure? Sometimes IDE may not be following the source code well. – m0hithreddy Jun 30 '20 at 09:24