Boost logo

Boost :

From: Peter Dimov (pdimov_at_[hidden])
Date: 2004-07-06 04:54:37


Howard Hinnant wrote:
>
> bool try_sale(rw_mutex& m)
> {
> read_lock rl(m);
>
> long balance = get_balance();
> long cost = get_total_cost();
>
> if (balance > cost)
> {
> write_lock wl(rl);
> set_balance(balance - cost);
> // ...
> return true;
> }
> return false
> }
>
> bool do_sale(rw_mutex& m)
> {
> while (true)
> {
> try_again:
> try {
> return try_sale(m);
> catch (rw_promotion_error&) {
> // Try the whole thing again
> // how is this implemented?
> goto try_again; // ?
> }
> }
> }
>
> vs this:
>
> void do_sale(rw_mutex& m)
> {
> read_lock rl(m);
>
> long balance = get_balance();
> long cost = get_total_cost();
>
> if (balance > cost)
> {
> rl.unlock(m);
> write_lock wl(m);
> set_balance(balance - cost);
> wl.transfer_to_read_lock(rl);
> balance = get_balance();
> }
> // ...
> }

Both are broken, unfortunately. :-)

The first one is broken, because the whole point of postponing the write
lock is to allow readers to operate at full speed. However, if readers
operate at full speed, then there is no guarantee that the write lock will
ever succeed; ergo, starvation. So promotion is good only for non-critical
updates.

The second one is also broken but I see that you corrected it in a follow-up
post.


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