Boost logo

Boost Users :

Subject: Re: [Boost-users] [Thread] order of shared_lock and unique_lock acquisitions
From: Steven Watanabe (watanabesj_at_[hidden])
Date: 2009-01-09 07:18:17


AMDG

Roland Bock wrote:
> Hi,
>
> I am trying to use shared_lock and unique_lock on a shared_mutex to
> allow read-functions to access a certain resource in parallel, but
> write functions use the resource exclusively.
>
> Is there a way to determine the order in which the (shared) locks are
> acquired? I tried testing with the attached program and got rather
> weird and (seemingly) non-deterministic results.
>
> In the test program (see attachment), I start 9 threads of readers(r)
> and writers(w). Before doing anything else, the sleep and wake up in
> groups.
>
> <snip>
>
> This I do not understand at all. The readers are using the shared
> mutex. Why is the second group waiting for the first to be finished?
> And how can the last writer be handled in second place?

First off, there is a race calculating the sleep times. There is absolutely
no guarantee that the readers and writers get any particular distribution
of wait times. For instance all the readers could get the early read times.
Not to mention that it is possible for all the threads to wait for 5
seconds.

In addition, while the reader or the writer does the second sleep,
(while still holding the lock)
all the other threads come out of their initial sleeps.

In Christ,
Steven Watanabe


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