|
Boost : |
From: Thomas Holenstein (tholenst_at_[hidden])
Date: 2000-08-09 17:25:13
> Ok, to help people get a feel for what we want, I've written up a
> strawman concept description for Mutex and ReentrantMutex.
>
> http://www.lsc.nd.edu/~jsiek/Mutex.html
Ok. When I could extend the class lock with notify() and wait(), I
can get something very much like a monitor with IMHO minimal effort.
I'll show you what I mean at the classical buffer: (This is mostly
stolen from Greg)
public class SharedBuffer {
int p, c, full;
vector buf;
public
buffer(int n) : p(0), c(0), full(0), buf(n) {}
void send(int m) {
X::lock l(*this);
if (full == buf.size()) l.wait();
buf[p] = m;
p = (p+1)%buf.size();
++full;
if (full == 1) l.notify();
}
int receive() {
X::lock l(*this);
if (full == 0) l.wait();
int i = buf[c];
c = (c + 1)%buf.size();
--full;
if (full == buf.size()-1) l.notify();
return i;
}
};
I like this idea, to be honest. It's just a very bit different than
Jeremys, but I think it adds much flexibility.
This way, you can get practical the same interface as with a monitor,
(but effectively, you are more flexible, as you don't need to lock
complete procedures)
If some people want a timeout, I'd suggest throwing an exception after
a specified time.
For wait and notify I would use a queue, as I always missed this in
Java.
There are some detail questions open: What happens with multiple
notifys, which thread enters first, the waiting or the locked, but I
think these are detail.
Thomas
Boost list run by bdawes at acm.org, gregod at cs.rpi.edu, cpdaniel at pacbell.net, john at johnmaddock.co.uk