|
Boost Users : |
Subject: Re: [Boost-users] circular buffer in a multithreaded program
From: Steffen Heil (Mailinglisten) (lists_at_[hidden])
Date: 2014-04-24 03:47:50
Hi
> You are right. Iterator holds a pointer to circular_buffer and accesses some of its internals.
> However it is safe to use only iterators and overwrite independent parts of circular_buffer. All the operations with iterators do not modify internals of circular_bufer.
> So if you change cb.erase_first(n)/cb.resize(cb.capacity()) to iterator operations there must be no problems. Though I thinks that's what you have done in case of custom vector-based data structure.
This might be a general C++ question and not be boost specific, but as I read the answer above the following question popped up:
If two threads read / write the same area in the memory and there is no synchronization, how is it ever guaranteed that they see the "current" values?
I see the problem, if one threads writes to a certain place in a buffer and another thread reads from that buffer, it might read cached values (either by the compiler keeping a value in a register or by the hardware keeping copies in cpu caches while the two threads run in different cpu cores / cpus).
So any kind of synchronization (that usually involves a memory barrier) is needed...
Coming from java there is a memory model that clearly specifies a "happens-before" relation. Values are guaranteed to be available if the operations in place are ordered according to that relation.
Is there any C++ specification for these things?
How much is it compiler / platform specific?
Regards,
Steffen
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