Boost logo

Boost :

Subject: Re: [boost] Fwd: [sync?] "Distributed" value (or something...)
From: Andrey Semashev (andrey.semashev_at_[hidden])
Date: 2014-07-30 05:14:59

On Wed, Jul 30, 2014 at 12:58 PM, Klaim - Joël Lamotte
<mjklaim_at_[hidden]> wrote:
> On Wed, Jul 30, 2014 at 10:45 AM, Andrey Semashev <andrey.semashev_at_[hidden]
>> wrote:
>> I think you can have a mutex-protected shared_ptr to the
>> value, which every reader can obtain and use. When the reader is not
>> using the value it demotes it to weak_ptr. Next time the reader wants
>> to use the object it attempts to promote it to shared_ptr, which will
>> only succeed if the value has not expired.
> That's close to the implementation I was thinking about but I also would
> like to avoid any lock if avoidable.
> (I lack practice in this domain to say if it's avoidable).

To be clear, that mutex would have to be locked only when the value is
changed (first by the writer to update the shared_ptr, then by every
reader when he discovers that his weak_ptr has expired) or the first
time when the value and readers are initialized. While the value is
stable readers don't lock the mutex and only work with their
weak_ptrs/shared_ptrs. If updates are rare, this can be a win.

>> This approach does not involve any callbacks, which might be a good or
>> bad thing depending on the use case.
> I think in my case the writes are so rarely changed but have so much impact,
> I'm assuming that it's more efficient if a callback is called once the
> value is written so that
> the systems don't have to test the value regularly.
> I might be wrong though, my application is not complete yet.

The problem with the callbacks is that you have to choose the thread
to invoke them in. In the simplest case this could be the writer
thread, in other cases a separate thread pool might be more desirable.

Boost list run by bdawes at, gregod at, cpdaniel at, john at