In this code
fn do_something_under_lock(
is_locked: &AtomicBool,
) {
loop {
let lock_acquired = is_locked
.compare_exchange(false, true, Ordering::Acquire, Ordering::Relaxed)
.is_ok();
if lock_acquired {
// Do something
// ...
is_locked.store(false, Ordering::Release);
break;
}
}
}
will moving let
outside of loop, like
fn do_something_under_lock(
is_locked: &AtomicBool,
) {
let mut lock_acquired;
loop {
lock_acquired = is_locked
.compare_exchange(false, true, Ordering::Acquire, Ordering::Relaxed)
.is_ok();
if lock_acquired {
// Do something
// ...
is_locked.store(false, Ordering::Release);
break;
}
}
}
give some performance benefit, will it change when Drop
is called(according to https://doc.rust-lang.org/rust-by-example/trait/drop.html#drop
The Drop trait only has one method: drop, which is called automatically when an object goes out of scope. The main use of the Drop trait is to free the resources that the implementor instance owns.
and at first glance, scope of lock_acquired
changed
), or will compiler produce same code anyway? What is the best way to check compiler output?
I expect compiler to understand that developer intent is the same and produce the same binary