Usually, parent thread who creates child thread, wants to get execution result (return value) of child thread.
For this, pthread_join() is used.
In most cases, pthread_create() / pthread_join() are used like a kind of pair.
So, habitually, developer tends to ignoring about detaching child thread.
But, in some cases, parent thread doesn't care about the result of child thread, and pthread_join() is not used.
In this case, child thread should be detached from parent thread to say that "parent thread doesn't care about the result".
Why this is required?
To pass return value of child thread, some memory should be used to store result until it is consumed.
So, detaching means avoiding keep that memory.
In POSIX thread, there are two ways for detaching thread.
one is using pthread_detach() function, and the other is using PTHREAD_CREATE_DETACHED attribute at pthread_create().
Always keep in mind that child thread keep some amount of memory to pass it's return value to parent.
Process version of this case is "Zombie Process". See this post for details.
'Language > C&C++' 카테고리의 다른 글
일반 file에 대한 select/poll... (0) | 2013.11.14 |
---|---|
Using allocated memory like 2-D array. (0) | 2013.07.10 |
GCC macro with variable number of arguements (0) | 2012.05.08 |
[C/C++/JAVA] 변수를 block중간에 선언하는 방법에 대한 단상. (0) | 2011.11.23 |
[C/C++] enable/disable function/macro with define switch. (0) | 2011.11.23 |