1

What I want to do is something on lines of...

pthread_create(&producer_thread, &to_join, producer_routine, &queue);
pthread_detach(producer_thread);
...
...
pthread_join(producer_thread, NULL);

Is this possible in some way, on running the above code, it is unable to join the thread.

ashfaq
  • 77
  • 1
  • 9
  • 2
    Are you familiar with the [XY problem](http://xyproblem.info)? – Iharob Al Asimi Aug 26 '16 at 21:35
  • 1
    No. Detaching a thread makes it permanently un-joinable. If you are in control of the thread function, however, then you can roll your own mechanism for waiting on specific detached threads. – John Bollinger Aug 26 '16 at 21:54
  • 1
    Always a good idea to read the [man page](http://man7.org/linux/man-pages/man3/pthread_detach.3.html): "Once a thread has been detached, it can't be joined with pthread_join(3) or be made joinable again." – kaylum Aug 26 '16 at 21:55
  • Related: http://stackoverflow.com/q/13917944/694576 – alk Aug 27 '16 at 14:52

1 Answers1

6

Once detached, it's not possible to join anymore.

From Notes on pthread_detach()'s man page:

Once a thread has been detached, it can't be joined with pthread_join(3) or be made joinable again.

alk
  • 69,737
  • 10
  • 105
  • 255