Quote:
Originally Posted by bkelly
I was unable to discover anything relevant from either of the two links. Did I miss something?
In general I have not recognized the answer. I'll reword:
Detach a thread if you need to <do this>. Otherwise do not detach it.
|
OK, let's try saying it another way...
This will appear to be a tautology, but please consider it seriously...
Quote:
Detach a thread if you [ do not ] need to < join it >. Otherwise do not detach it.
|
Which implicitly includes the question...
Code:
When or why might I need to join a thread?
And again, the short answer to that is: When you need to
get the exit status of a thread, or when you need to
block one thread until another completes, or if you want to inherit or
share scheduling priority among threads, then you will
need to join them.
Which seems to not be the answer you are looking for, I suspect because you are really asking, "When I write some arbitrary threaded application, how can I decide when to make use of the
joinable or
deteched state of threads?".
And the answer to that takes us back to the
model of your application - how your model makes use of threads during execution. In other words, the answer depends on where the thread fits into the application's execution model.
Where OpenMP* fits into this picture is explained in their
About Us page...
Quote:
... the OpenMP API is a portable, scalable model that gives shared-memory parallel programmers a simple and flexible interface for developing parallel applications...
|
Similarly, if you were to ask, "When should I use an
integer type vs a
float type for a variable in my program?", the answer is of course, "It depends on what you intend your program to do with it after you create it!".
I also found this
StackOverflow page which includes another brief discussion and may be helpful.
Hope this helps!
*Note: I am not really familiar with OpenMP and not particularly recommending it, but it is one model for programming threaded applications already mentioned and as such is an example at hand - there are many more. The point being made is the ultimate dependence of the answer upon the chosen model.