Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.programming.threads > #1431

Re: Are detached threads useful?

From andrew@cucumber.demon.co.uk (Andrew Gabriel)
Newsgroups comp.programming.threads
Subject Re: Are detached threads useful?
Date 2013-03-18 14:57 +0000
Organization A noiseless patient Spider
Message-ID <ki7a0g$17d$1@dont-email.me> (permalink)
References <cfd2e5c1-5c00-4ebb-9da1-a3588dc41d79@w14g2000vba.googlegroups.com> <kgt546$s4b$1@dont-email.me> <a878aa28-916b-477f-993f-92cf72b0f322@y4g2000yqa.googlegroups.com>

Show all headers | View raw


In article <a878aa28-916b-477f-993f-92cf72b0f322@y4g2000yqa.googlegroups.com>,
	Michael Podolsky <michael.podolsky.69@gmail.com> writes:
> On Mar 2, 10:16 am, and...@cucumber.demon.co.uk (Andrew Gabriel)
> wrote:
>>
>> Looking through source I've written for various things, these
>> are the two cases where I've used pthread_detach() (each several
>> times in different programs)...
>>
>> 1)
>> A thread which is about to exit, and no other thread in the
>> process cares or needs to know, so the sequence is:
>>      pthread_detach(pthread_self());
>>      pthread_exit(NULL);
>> This is to avoid leaving a detached/zombie thread around which
>> will never be reaped - just a matter of tidyness really, although
>> it could become important if a process does this repeatedly that
>> it isn't leaking zombie threadids (none of mine do it frequently).
> This scenario is not clear for me. What if our program needs to exit
> before your thread exits? Then, to shutdown gracefully and for a
> general case we need to tell the thread to finish and to join it. We
> simply cannot be sure that this thread will exit always by itself
> before our program may decide to finish.

The scenarios I've used this are:

The main thread starts up all the program threads, but is no longer
needed after doing that initialisation, and gracefully bows out.

If you need the threads in the program to be created in a specific
way (such as PTHREAD_SCOPE_SYSTEM), you can't rely on the main thread
having been created in the way you need, and have to explicitly create
all the threads in the way required, after which the main thread may no
longer be any use. (Subset of the case above).

A reader thread finishes reading, and bows out, whilst processing and
writer threads continue handling the tail end of the data stream.

>> 2)
>> A thread which performs only a monitoring/reporting function
>> for the process, which simply continues to run until all the
>> non-detached threads finish and exit.
> Yes, this is clear. Yet my claim in my first mail was that the thread
> which used to be a simply 'reporting' thread may eventually become a
> thread with more duties which may not be just terminated. Even for a
> simply reporting thread we might want at least to flush file buffers
> of stdio library.

That's done in the exit(3c) anyway. stdio buffers are global resources.

>> Did you have specific uses of pthread_detach in mind which you
>> regard as wrong?
> There may be some correct uses, of course. Like there may be some
> correct uses of MS Windows Events for writing multithread. )))
> =Michael

-- 
Andrew Gabriel
[email address is not usable -- followup in the newsgroup]

Back to comp.programming.threads | Previous | NextPrevious in thread | Next in thread | Find similar


Thread

Are detached threads useful? Michael Podolsky <michael.podolsky.69@gmail.com> - 2013-02-14 18:52 -0800
  Re: Are detached threads useful? Marcel Müller <news.5.maazl@spamgourmet.org> - 2013-02-16 11:00 +0100
  Re: Are detached threads useful? andrew@cucumber.demon.co.uk (Andrew Gabriel) - 2013-03-02 15:16 +0000
    Re: Are detached threads useful? Michael Podolsky <michael.podolsky.69@gmail.com> - 2013-03-07 22:27 -0800
      Re: Are detached threads useful? andrew@cucumber.demon.co.uk (Andrew Gabriel) - 2013-03-18 14:57 +0000
  Re: Are detached threads useful? Gerald Breuer <Gerald.Breuer@googlemail.com> - 2013-03-08 19:11 +0100

csiph-web