Boost logo

Boost :

From: Greg Colvin (gcolvin_at_[hidden])
Date: 2001-06-27 16:18:58


From: <williamkempf_at_[hidden]>
>...
> Let me summarize the two main interfaces that people seem to think
> are the best, and try and flesh them out completely, adding more
> questions as I go.

Thanks for this clear exposition. There is (at least) a third,
combined possibility (leaving aside free versus member functions).
It has the possible advantage of not requiring any overhead for
reference counting or heap allocation when the thread::ref interface
is not needed. It has the disadvantage of redundancy.

   class thread : noncopyable
   {
   public:

      class ref
      {
      public:
         ref();
         ref(const ref& other);
         ~ref();

         ref& swap(const ref& other);
         ref& operator=(const ref& other);

         bool operator==(const ref& other) const;
         bool operator!=(const ref& other) const;

         bool is_current() const;
         bool is_alive() const;

         void join();
      };

      static ref create(boost::function1<void>);
      static void sleep(const xtime& xt);
      static void yield();

      thread(boost::function1<void>); // start a thread running
      ~thread(); // detach from thread if still running

       bool is_current() const;
       bool is_alive() const;
      void join();
   };

The only easy way I see to avoid the redundancy is

   class thread : noncopyable
   {
   public:

      static shared_ptr<thread> create(boost::function1<void>);

      static void sleep(const xtime& xt);
      static void yield();

      thread(boost::function1<void>); // start a thread running
      ~thread(); // detach from thread if still running

       bool is_current() const;
       bool is_alive() const;
       void join();
   };

Although the above would require a thread-safe shared_ptr that
might be less efficient than the native handle, so perhaps it
should be:

   class thread : noncopyable
   {
   public:

      class ptr
      {
      public:
         thread* operator->();
         ...
      };

      static ptr create(boost::function1<void>);

      ...
   };

How important it is that Gary not pay for (possible) reference
counting he doesn't need I don't know, so I'll leave it to
Gary to actually argue for one of these if he wants. I remain
satisfied with interface 2.


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