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
use std::sync::atomic::AtomicBool;use std::sync::atomic::fence;use std::sync::atomic::Ordering;// A mutual exclusion primitive based on spinlock.pubstructMutex{flag:AtomicBool,}implMutex{pubfnnew() -> Mutex{Mutex{flag:AtomicBool::new(false),}}pubfnlock(&self){while !self.flag.compare_and_swap(false,true,Ordering::Relaxed){}// This fence synchronizes-with store in `unlock`.fence(Ordering::Acquire);}pubfnunlock(&self){self.flag.store(false,Ordering::Release);}}
The CAS function would return the original value of the flag, so the loop would exit when the original value of flag is true, i.e. when it is locked.
The text was updated successfully, but these errors were encountered:
jyn514
added
A-docs
Area: documentation for any part of the project, including the compiler, standard library, and tools
C-bug
Category: This is a bug.
labels
Jul 27, 2020
Page: https://doc.rust-lang.org/std/sync/atomic/fn.fence.html
The CAS function would return the original value of the
flag
, so the loop would exit when the original value offlag
istrue
, i.e. when it is locked.The text was updated successfully, but these errors were encountered: