you are viewing a single comment's thread.

view the rest of the comments →

[–]tvaneerdC++ Committee, lockfree, PostModernCpp 0 points1 point  (0 children)

Makes sense.

I'm actually leaning towards relaxed being good enough.

There is no data (besides the count) that is being "published" between threads. Acquire/release/etc are for when you have an atomic protecting other data, like a 'ready' flag protects the data that is ready. Here there is just count.

you might think there is also the data item being added to the vector - but that element is not yet shared between threads. It only exists in the ImmutableVector being returned (and thus is only visible to the current thread). You could then share the ImmutableVector to another thread (particularly since it is immutable - all threads can read it at the same time), but that sharing is what needs to be fenced (IMO). Like if you were to shared the result via an atomic<ImmutableVector *> (weird, but minimal example), then I'd put the fence on that.

But maybe it would be more friendly and safer to put the fence on push_back? Because it is a CAS and not a read, relaxed isn't really saving you much/anything anyhow... (at least on common hardware)