Boost logo

Boost :

From: Greg Colvin (gcolvin_at_[hidden])
Date: 2000-08-10 16:46:03


From: <scleary_at_[hidden]>
> > I don't want a primitive layer, because I have seen the damage
> > such primitives allow.
>
> Again, what about the library-writer's perspective? I want a simple,
> as-fast-as-possible Mutex that I will use in a way totally hidden from user
> code. I don't need [the overhead of] a monitor for this.

Here is more efficient but just-as-safe refinement.

For the case where no waiting or signaling is required we
have what can be a simple wrapper for a mutex, where the
entry constructor locks and the entry destructor unlocks
the mutex:

   class shared {
   protected:
      struct entry {
         entry(shared*);
         ~entry();
      };
      ~shared();
   };

When a full monitor with waiting and signaling is required
we have a possibly heavier class:

   class shared_condition {
   protected:
      struct entry {
         entry(shared_condition*,lambda_functor);
         ~entry();
      };
      ~shared_condition();
   };

The use is as follows. The entry constructor does the
loop-waiting-for-condition, and the entry destructor
signals the condition:

   class buffer : shared_condition {
      int p,c,full;
      vector<int> buf;
   public:
      buffer(int n) : p(0),c(0),full(0),buf(n){}
      void send (int m) {
         entry wait(this,(free1 == buf.size())(full));
         buf[p] = m;
         p = (p+1)%buf.size();
         ++full;
      }
      int receive() {
         entry wait(this,(free1 == 0)(full));
         int i = buf[c];
         c = (c+1)%buf.size();
         --full;
         return i;
      }
   };


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