UNIX Socket FAQ

A forum for questions and answers about network programming on Linux and all other Unix-like systems

You are not logged in.

#1 2007-08-23 05:27 PM

zhn636
Member
Registered: 2007-04-24
Posts: 146

Re: thread detached

"A thread's underlying storage can be reclaimed immediately on termination if that thread has been detached",can you tell me what is the meaning of this sentence ,and what is the thread detached?
and how we can detach a thread?
thanks ,best wishes

Offline

#2 2007-08-23 05:55 PM

HectorLasso
Administrator
From: Colombia
Registered: 2002-06-12
Posts: 353

Re: thread detached

A thread can be detached by using "pthread_detach()" read the man page... If you create the thread in the detached state, then you won't need to run pthread_join() or pthread_detach().

The main difference between a detached thread an a joinable thread is that you don't need to join (pthread_join) the thread to release its storage resources after pthread_exit() has been called. Of course you can also call pthread_detach() instead of pthread_join() but you won't have access to the exit status.

Read the man pages for pthread_detach(), pthread_create(), pthread_attr_setdetachstate() for more information.

Do you know what a thread is? Read this: Thread  article.

If you are wondering why not use pthread_join() instead of pthread_detach() and just not look at the exit status, well, the answer is that pthread_join() will wait for the thread to finish if it hasn't already, thus blocking the calling thread until the other one finishes. pthread_detach() is like saying: "I don't care about this thread anymore, so when it dies (if it hasn't already) please send the exist status to the trash can... I won't be waiting for that to happen..."

Offline

#3 2007-08-23 06:02 PM

zhn636
Member
Registered: 2007-04-24
Posts: 146

Re: thread detached

oh,can you tell me why we put a thread in the detached state? what are the differences between these threads and the thread in the normal state?

thanks

Offline

#4 2007-08-25 07:42 AM

mlampkin
Administrator
From: Sol 3
Registered: 2002-06-12
Posts: 911
Website

Re: thread detached

If you DO NOT care about WHY you thread is exiting, then perform a detach operation...

If you DO care about WHY you thread is exiting, the either do NOT detach and perform a join operation... OR detach BUT make certain that there is a global repository for thread results and that every thread calls it ( and deposits their result ) prior to exiting...

Of course - tis just my opinion...


Michael


"The only difference between me and a madman is that I'm not mad."

Salvador Dali (1904-1989)

Offline

#5 2009-03-10 02:04 PM

overflow
Member
Registered: 2009-03-10
Posts: 2

Re: thread detached

Hello
I am going to open an old discussion, but i think this is an important information.

1) Is there any differences about the structures allocated by the kernel between a detached thread or a non-detached thread
2) Are the data structures of a non-detached thread deallocated automatically after the  thread termination, or you have to call pthread_join()?

Offline

#6 2009-03-10 03:18 PM

i3839
Oddministrator
From: Amsterdam
Registered: 2003-06-07
Posts: 2,230

Re: thread detached

1. No, it only influences thread exit behaviour.

2. An exited, but not joined thread is comparable to a zombie process. So
although most resources are gone, you still need to call pthread_join.

If you can't always join a thread then make it detached, except if the
whole program exits, in which case it doesn't matter. Not doing a join
is just messy.

Of course there are exceptions, e.g. if it only happens a limited amount
of times in rare circumstances, when it's not worth worrying about it and
when doing it the "proper" just complicates the code.

Offline

#7 2009-03-10 03:42 PM

overflow
Member
Registered: 2009-03-10
Posts: 2

Re: thread detached

Thanks for the quick answer

I didn't know that you have deallocate a non detached thread... thanks for the information!

i3839;26142 wrote:


If you can't always join a thread then make it detached, except if the
whole program exits, in which case it doesn't matter. Not doing a join
is just messy.

Why if the whole program exits you don't have worry about threads deallocation?
Take a simple c program as example that create some threads. In this case every thread is a child of the void main(). So as you said, you have to manage also the deallocation of it when they finishes.
Is the void main finishes earlier, all the children will became zombies so I think you need to deallocated them. Did I do some mistakes?

Offline

#8 2009-03-10 04:39 PM

i3839
Oddministrator
From: Amsterdam
Registered: 2003-06-07
Posts: 2,230

Re: thread detached

I'm not sure what you mean with deallocating a thread. A thread is (mostly)
gone when it calls exit or when it is killed, just like with processes, where
a minimal zombie process is kept around till the parent calls wait().

From the kernel's point of view, when a joinable thread exits it needs to
keep around the exit value, among other things. So it can't free all used
resources until you called pthread_join(). On the user space side, the
pthread implementation may also keep resources around per thread,
which it can only free after a join (whether this is the case or not depends
on the implementation).

In Linux a thread is more or less exactly the same as a process, but one
which shares more resources other processes, most importantly the memory
address space is shared.

Is the void main finishes earlier, all the children will became zombies so I think you need to deallocated them. Did I do some mistakes?


It's a bit fuzzy, and the main thread is a bit special, kind off. If the main
function returns the program will exit, bringing down all threads. But they
won't hang around as zombies, because the whole thread group is gone.
Other threads than the main one can call pthread_join for exited threads
as far as I know, so even if the main thread is gone you need to join
undetached threads. But in that case it's better to just make those threads
detached, or to kill them before the main thread exits. I don't know all
the detailed behaviour, and that can change between implementations.
It's best to keep things somewhat simple and to not assume too much.

Offline

#9 2009-03-10 08:35 PM

Nope
Administrator
From: Germany
Registered: 2004-01-24
Posts: 385
Website

Re: thread detached

Let's take a look at the different scenarios:

Normal threads' resources are freed
- after the thread has exited and the main thread called pthread_join
- in case the main thread exits

Detached thread's resources are freed
- right after it exits/is finished
- in case the main thread exits

So, no matter what you do, if the main thread exits, it takes all its child threads with it.

What is a normal thread good for....
Your main thread on a 4 core computer has to do N independent calculations and needs all of the results before it can continue. So it starts N threads and then waits in a join loop until all N results are ready. This way it can use all available cores and the program logic needing all of the results will not be broken.

What is a detached thread good for...
You have a simple http server and use a thread for every incoming connection. The thread that's created is detached as the main thread doesn't need to know anything after the child thread is created successfully.

Those examples are simplified of course, but you should get the drift...

Offline

#10 2011-04-08 08:02 PM

weldie
Guest

Re: thread detached

Loco wrote:

A thread can be detached by using "pthread_detach()" read the man page... If you create the thread in the detached state, then you won't need to run pthread_join() or pthread_detach().

The main difference between a detached thread an a joinable thread is that you don't need to join (pthread_join) the thread to release its storage resources after pthread_exit() has been called. Of course you can also call pthread_detach() instead of pthread_join() but you won't have access to the exit status.

Read the man pages for pthread_detach(), pthread_create(), pthread_attr_setdetachstate() for more information.

Do you know what a thread is? Read this: Thread  article.

If you are wondering why not use pthread_join() instead of pthread_detach() and just not look at the exit status, well, the answer is that pthread_join() will wait for the thread to finish if it hasn't already, thus blocking the calling thread until the other one finishes. pthread_detach() is like saying: "I don't care about this thread anymore, so when it dies (if it hasn't already) please send the exist status to the trash can... I won't be waiting for that to happen..."




Thanks for your clear explanation!! like it

#11 2011-04-08 09:06 PM

weldie
Guest

Re: thread detached

Thank you Loco for your clear explanation

Board footer

Powered by FluxBB