Fwd: [sync?] "Distributed" value (or something...)
---------- Forwarded message ----------
From: Klaim - Joël Lamotte
On Wed, Jul 30, 2014 at 1:51 AM, Klaim - Joël Lamotte
I would like to ask Boost developers and users:
1. Am I the only one seeing the following pattern in my concurrent systems code? 2. Is there already some tools available in Boost that does what I describe here and that I am not aware of? 3. If not, would there be any interest in the tool I'm describing here or not? Is it a dumb idea? (I keep getting back to this idea but I'm surprised not finding anything similar online)
The pattern that I am seeing occurs when: 1. I have a value or a set of values in an object or several objects which value(s) describe some kind of configuration that several other systems will read and use. 2. The object(s) will be updated only by one thread (at a time). 3. When the object's value is modified, all the systems that rely on the object's value have to be notified so that they update themselves using that value. 4. The systems I am refering to can use different threads for updating and we can assume that they are running concurrently. 5. It is not important that the source object's value is synched with the reader's values at all time, however it is important that the sequence of changes to the source object's value is signaled to the readers exactly in the same order and "as fast as possible", but delay is ok.
I'm not sure I fully understood the idea but is it implementable in RCU manner? 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 expired. This approach does not involve any callbacks, which might be a good or bad thing depending on the use case. In any case, I think this would be a fairly high level primitive (especially if async callbacks are involved), so I'm not sure Boost.Sync is the right place for it.
On Wed, Jul 30, 2014 at 12:45 PM, Andrey Semashev
On Wed, Jul 30, 2014 at 1:51 AM, Klaim - Joël Lamotte
wrote: I would like to ask Boost developers and users:
1. Am I the only one seeing the following pattern in my concurrent systems code? 2. Is there already some tools available in Boost that does what I describe here and that I am not aware of? 3. If not, would there be any interest in the tool I'm describing here or not? Is it a dumb idea? (I keep getting back to this idea but I'm surprised not finding anything similar online)
The pattern that I am seeing occurs when: 1. I have a value or a set of values in an object or several objects which value(s) describe some kind of configuration that several other systems will read and use. 2. The object(s) will be updated only by one thread (at a time). 3. When the object's value is modified, all the systems that rely on the object's value have to be notified so that they update themselves using that value. 4. The systems I am refering to can use different threads for updating and we can assume that they are running concurrently. 5. It is not important that the source object's value is synched with the reader's values at all time, however it is important that the sequence of changes to the source object's value is signaled to the readers exactly in the same order and "as fast as possible", but delay is ok.
I'm not sure I fully understood the idea but is it implementable in RCU manner? 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 expired.
Read that as "has not expired".
This approach does not involve any callbacks, which might be a good or bad thing depending on the use case. In any case, I think this would be a fairly high level primitive (especially if async callbacks are involved), so I'm not sure Boost.Sync is the right place for it.
On Wed, Jul 30, 2014 at 10:45 AM, Andrey Semashev wrote: I'm not sure I fully understood the idea but is it implementable in
RCU manner? Thanks for the keyword, I think it's indeed related but I wasn't aware of
the idea.
I'll have to read more (http://en.wikipedia.org/wiki/Read-copy-update) to
be sure. 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). 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. In any case, I think this would
be a fairly high level primitive (especially if async callbacks are
involved), so I'm not sure Boost.Sync is the right place for it. I was assuming it would be close to synchronized_value but it's a bit more
high-level indeed. _______________________________________________
Unsubscribe & other changes:
http://lists.boost.org/mailman/listinfo.cgi/boost
On Wed, Jul 30, 2014 at 12:58 PM, Klaim - Joël Lamotte
On Wed, Jul 30, 2014 at 10:45 AM, Andrey Semashev
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.
On Wed, Jul 30, 2014 at 11:14 AM, Andrey Semashev wrote: 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. Indeed. In the general case, a nice interface would let the user decide how
the callback would be called.
Maybe using an optional Executor concept object as first argument to the
callback subscription function
would be enough (as for future.then() )
In my specific use case, just calling the callbacks in the thread that
update the value is ok
because I always assume (in all my code) that a callback will do it's own
synchronization work if it's necessary.
That's why in my initial example there is a work queues used in the
callback.
Now that I think about it, using an executor is a generalization of the
same idea, only
the point of decision of how to execute the callback's code is changed.
Assuming the work queue match the Executor concept, this code:
m_foo_count.on_changed( [this]( int new_value ){
m_work_queue( [=]{
on_foo_count_changed( new_value );
});
});
could be simplified to:
m_foo_count.on_changed( m_work_queue, [this]( int new_value ){
on_foo_count_changed( new_value );
});
(or the equivalent using bind)
But the generated code would be like the first snippet, just a wrapping.
Andrey Semashev wrote:
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.
I'd say that the problem with the callback architecture is that you still need to communicate the change to the readers, which would require them to still poll an atomic variable at the very least. And if they poll, there's no need for callbacks. Just have the writer increment a version counter. weak_ptr's would be slightly less performant because they do a CAS on lock, instead of just an atomic read. I'm not sure if this would matter that much though.
On Wed, Jul 30, 2014 at 11:36 AM, Peter Dimov
I'd say that the problem with the callback architecture is that you still need to communicate the change to the readers, which would require them to still poll an atomic variable at the very least. And if they poll, there's no need for callbacks. Just have the writer increment a version counter. weak_ptr's would be slightly less performant because they do a CAS on lock, instead of just an atomic read. I'm not sure if this would matter that much though.
If a copy of the new value is passed as argument to the callbacks, then I don't see the problem - except the cost of the copy of course.
participants (3)
-
Andrey Semashev
-
Klaim - Joël Lamotte
-
Peter Dimov