Boost logo

Boost Users :

From: Alexander Terekhov (yg-boost-users_at_[hidden])
Date: 2003-05-21 14:04:49


"William E. Kempf" wrote:
[...]
> >> How about moving this discussion to c.p.t.?
> >
> > Well, just in case... <Forward Quoted>
>
> Thanks... I currently can't access c.p.t. in any reasonable manner. I'm
> working to rectify this,

http://news.cis.dfn.de might help. ;-)

                           but in the mean time, I appreciate the cross
> post.

I'll wait a day or two and post a reply addressing some of your points
to comp.programming.threads. You might also want to keep an eye on the
discussion of "validity" of the following DCSI** pattern (not exposing
thread_specific_ptr::release() or thread_specific_ptr::reset() to the
clients... Butenhof believes that since <quote>You at least need to be
sure all threads are "done" with the current key and will never read it
again, because pthread_key_delete() cannot invalidate the key</quote>,
the clients ("individual threads") should <quote>confirm that
synchronization by clearing their value for the key</quote>).

class stuff { /* ... */ };

  class thing {
  public:
    thing(/* ... */) : /* ... */ stuff_shared_ptr(0) /* ... */ { /*...*/ }
   ~thing() { /* ... */ delete stuff_shared_ptr; /* ... */ }
    /* ... */
    const stuff & stuff_instance();
    /* ... */
  private:
    /* ... */
    mutex stuff_mtx;
    stuff * stuff_shared_ptr;
    thread_specific_ptr<stuff, no_cleanup> stuff_thread_ptr;
    /* ... */
  };

  const stuff & thing::stuff_instance() { // "lazy" one
    stuff * ptr;
    if (0 == (ptr = stuff_thread_ptr.get())) {
      { mutex::guard guard(stuff_mtx);
        if (0 == (ptr = stuff_shared_ptr))
          ptr = stuff_shared_ptr = new stuff(/*...*/);
      }
      stuff_thread_ptr.set(ptr);
    }
    return *ptr;
  }

the details can be found in "Double-checked locking and memory barriers"
c.p.t. thread.

regards,
alexander.

**) DCSI stands for double-checked serialized initialization. Two variations
    are known corrently: DCSI-MBR and DCS-TSD (TLS). There's also another
    DC-"pattern" -- double-checked concurrent initialization [DCCI]; using
    atomic<> with barriers and "CAS".

--
"// Possible implementation for <pthread.h> and <cthread>
 #include <thread>
 // for <cthread>, please remove the using-directive
 using namespace std;"
                                                   -- ...

Boost-users list run by williamkempf at hotmail.com, kalb at libertysoft.com, bjorn.karlsson at readsoft.com, gregod at cs.rpi.edu, wekempf at cox.net