Boost logo

Boost :

From: Greg Colvin (gcolvin_at_[hidden])
Date: 2001-06-27 11:01:01


From: <williamkempf_at_[hidden]>
> --- In boost_at_y..., "Greg Colvin" <gcolvin_at_u...> wrote:
> > ...
> > You can let the OS do the counting:
> >
> >
> > class thread_ref {
> > HANDLE id;
> > public:
> > thread_ref(const thread_ref& r) {
> > if (!DuplicateHandle(GetCurrentProcess(),r.id,
> > GetCurrentProcess(),&id,
> > 0,false,DUPLICATE_SAME_ACCESS))
> > throw something;
> > }
> > thread_ref& operator=(const thread_ref& r) {
> > if (&r != this) {
> > this->~thread_ref();
> > new(this) thread_ref(r);
> > }
> > }
> > ~thread_ref() { CloseHandle(id); }
> >
> > void join();
> > ...
> >
> > };
>
> Not all platforms can handle this in the same way, though, including
> pthreads.

Right, and on those platforms you can count explicitly. For
all I know DuplicateHandle is more expensive than an explicit
count even on Windows, but I suspect not, especially once you
make the counter thread safe. Although to be thread safe the
assignment probably needs to be:

   thread_ref& operator=(const thread_ref& r) {
      HANDLE new;
      if (!DuplicateHandle(GetCurrentProcess(),r.id,
                           GetCurrentProcess(),&new,
                           0,false,DUPLICATE_SAME_ACCESS))
      CloseHandle(id);
      id = new;
   }


Boost list run by bdawes at acm.org, gregod at cs.rpi.edu, cpdaniel at pacbell.net, john at johnmaddock.co.uk