1

We have a class that has a protected pthread_t variable. Once that class has been constucted, the pthread_t exists, but it hasn't had an ID assigned to it which is what pthread_create does.

Is it OK to call pthread_join on the pthread_t variable in this case?

If I understand the man page for pthread_join correctly it should return a ESRCH error but this man page can be interpreted differently.

Lieuwe
  • 1,734
  • 2
  • 27
  • 41
  • How do you know your `pthread_t` variable doesn't contain the ID of a thread that has been created, thus causing the caller to wait for the wrong thread? – Andrew Henle Jan 12 '16 at 13:13
  • Good point. Somehow I assumed the pthread_t would be created and the ID initialised to an ID that was 0 / not possible. Is it possible to look at pthread_t and check if the ID has been set? – Lieuwe Jan 12 '16 at 13:28
  • *Is it possible to look at pthread_t and check if the ID has been set?* You can use a debugger or emit a log message of some type with the value of the ID. How do you initialize the value? – Andrew Henle Jan 12 '16 at 13:37
  • I don't know how it is initialised. Pthread_create sets that ID according to the man page. I am just trying to avoid carrying an 'initialised' flag around. If I can call pthread_join on a pthread_t that has not been used in conjunction with pthread_create then thats great. If I can somehow look at a pthread_t object and check myself if it has been 'created' then that is fine too. – Lieuwe Jan 12 '16 at 14:01
  • Your `pthread_t` value has to start with some value - it's either indeterminate or initialized to a specific value. – Andrew Henle Jan 13 '16 at 12:28

1 Answers1

1

pthread_t is nothing but unsigned long int, thus during declaration initialize it with 0 value. Now just before pthread_join just check if it is non zero.If it is zero then pthread_create is not called.

Arun Pal
  • 687
  • 7
  • 28