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

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,239

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

Offline

#8 2009-03-10 04:39 PM

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

Re: thread detached

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

#11 2011-04-08 09:06 PM

weldie
Guest

Re: thread detached

Thank you Loco for your clear explanation

Board footer

Powered by FluxBB