should we explicitly join a thread to finish? I mean开发者_JS百科 is this like process world: when we don't wait for them to finish we got zombie processes.
Not necessarily; you can always detach a thread if you don't wan't to explicitly wait for it.
There are a few things to consider:
- If you don't join a non-detached thread it will become a zombie thread (yes, there are such things)
- If you don't join a thread, your main thread might end before the "child" ends
- Once you detach a thread there is no way to attach it back
How do you detach a thread ? There are two ways:
detachstate = 1;
rc = pthread_attr_setdetachstate(&attr, detachstate);
if (rc) {
handle error;
}
pthread_create(&thr, &attr, th_fun, NULL);
And the second way
void *th_fun(void *arg)
{
pthread_detach(pthread_self());
}
精彩评论