You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
I found this curious, as clearly this operation may mutate the u32 inside. So why is it not accepting an &mut self?
Looking at the implementation, I see why this works. It boils down to a call to atomic_compare_exchange, which is unsafe thus (I think) allowing the immutable reference to coerce to a *mut.
But should the outward facing interface accept an &mut self to:
Aid the mental model of the programmer?
Prevent the ability to hold multiple mutable references.
Thoughts?
The text was updated successfully, but these errors were encountered:
vext01
changed the title
Should operations on atomics which mutate via raw pointers accept a mutible reference to self?
Should operations on atomics which mutate via raw pointers accept a mutable reference to self?
May 1, 2018
You can only obtain a &mut self if you have unique access to the value. The entire point of the atomic types is to be used in contexts where multiple threads have concurrent access to the value.
Hi,
I was looking at the
AtomicU32
documentation. The signature forcompare_and_swap
is:I found this curious, as clearly this operation may mutate the
u32
inside. So why is it not accepting an&mut self
?Looking at the implementation, I see why this works. It boils down to a call to
atomic_compare_exchange
, which is unsafe thus (I think) allowing the immutable reference to coerce to a*mut
.But should the outward facing interface accept an
&mut self
to:Thoughts?
The text was updated successfully, but these errors were encountered: