Skip to content

Instantly share code, notes, and snippets.

@cfsamson
Last active January 3, 2020 22:44
Show Gist options
  • Save cfsamson/06d473b476628d315828bafc69bf6b5a to your computer and use it in GitHub Desktop.
Save cfsamson/06d473b476628d315828bafc69bf6b5a to your computer and use it in GitHub Desktop.
use std::thread;
use std::sync::atomic::{AtomicBool, Ordering};
const LOCKED: AtomicBool = AtomicBool::new(false);
static mut COUNTER: usize = 0;
#[inline(always)]
pub fn test(inc: usize) {
while LOCKED.compare_and_swap(true, false, Ordering::Relaxed) {
}
unsafe { COUNTER += inc};
LOCKED.store(false, Ordering::Relaxed);
}
fn main() {
let t1 = thread::spawn(move || {
for _ in 0..100_000 {
test(1);
}
});
let t2 = thread::spawn(move || {
for _ in 0..100_000 {
test(1);
}
});
t1.join().unwrap();
t2.join().unwrap();
println!("{}", unsafe { COUNTER });
}
#![feature(asm)]
use std::thread;
static mut LOCKED: bool = false;
static mut COUNTER: usize = 0;
pub fn test(inc: usize) -> usize {
while unsafe { LOCKED } {}
unsafe { LOCKED = true };
unsafe { COUNTER += inc};
unsafe { LOCKED = false };
unsafe { COUNTER }
}
fn main() {
let mut handles = vec![];
for i in 0..5 {
let handle = thread::spawn(move || {
let mut prevent_optimization = Vec::with_capacity(100_000);
for _ in 0..100_000 {
let res = test(1);
prevent_optimization.push(res);
}
let last_val = prevent_optimization.last().unwrap();
println!("{} finished. Last value: {}", i + 1, last_val);
});
handles.push(handle);
}
for handle in handles {
handle.join().unwrap();
}
println!("{}", unsafe { COUNTER });
}
use std::sync::atomic::{AtomicBool, Ordering};
use std::thread;
static LOCKED: AtomicBool = AtomicBool::new(false);
static mut COUNTER: usize = 0;
pub fn test(inc: usize) -> usize {
while LOCKED.load(Ordering::Relaxed) {}
LOCKED.store(true, Ordering::Relaxed);
unsafe { COUNTER += inc };
LOCKED.store(false, Ordering::Relaxed);
unsafe { COUNTER }
}
fn main() {
let mut handles = vec![];
for i in 0..5 {
let handle = thread::spawn(move || {
let mut prevent_optimization = Vec::with_capacity(100_000);
for _ in 0..100_000 {
let res = test(1);
prevent_optimization.push(res);
}
let last_val = prevent_optimization.last().unwrap();
println!("{} finished. Last value: {}", i + 1, last_val);
});
handles.push(handle);
}
for handle in handles {
handle.join().unwrap();
}
println!("{}", unsafe { COUNTER });
}
use std::sync::atomic::{AtomicBool, Ordering};
use std::thread;
static LOCKED: AtomicBool = AtomicBool::new(false);
static mut COUNTER: usize = 0;
pub fn test(inc: usize) -> usize {
while LOCKED.compare_and_swap(true, false, Ordering::Relaxed) {
}
unsafe { COUNTER += inc};
LOCKED.store(false, Ordering::Relaxed);
unsafe { COUNTER }
}
fn main() {
let mut handles = vec![];
for i in 0..5 {
let handle = thread::spawn(move || {
let mut prevent_optimization = Vec::with_capacity(100_000);
for _ in 0..100_000 {
let res = test(1);
prevent_optimization.push(res);
}
let last_val = prevent_optimization.last().unwrap();
println!("{} finished. Last value: {}", i + 1, last_val);
});
handles.push(handle);
}
for handle in handles {
handle.join().unwrap();
}
println!("{}", unsafe { COUNTER });
}
use std::sync::atomic::{AtomicBool, Ordering};
use std::thread;
static LOCKED: AtomicBool = AtomicBool::new(false);
static mut COUNTER: usize = 0;
pub fn test(inc: usize) -> usize {
while LOCKED.compare_and_swap(false, true, Ordering::Acquire) {}
unsafe { COUNTER += inc };
LOCKED.store(false, Ordering::Release);
unsafe { COUNTER }
}
fn main() {
let mut handles = vec![];
for i in 0..5 {
let handle = thread::spawn(move || {
let mut prevent_optimization = Vec::with_capacity(100_000);
for _ in 0..100_000 {
let res = test(1);
prevent_optimization.push(res);
}
let last_val = prevent_optimization.last().unwrap();
println!("{} finished. Last value: {}", i + 1, last_val);
});
handles.push(handle);
}
for handle in handles {
handle.join().unwrap();
}
println!("{}", unsafe { COUNTER });
}
Example 1:
Debug
1 finished. Last value: 219364
3 finished. Last value: 228838
2 finished. Last value: 229637
4 finished. Last value: 247015
5 finished. Last value: 247637
247637
Release
1 finished. Last value: 109870
2 finished. Last value: 198109
3 finished. Last value: 301953
4 finished. Last value: 393877
5 finished. Last value: 470852
470852
Example 2:
Debug
3 finished. Last value: 265995
1 finished. Last value: 271110
2 finished. Last value: 272634
4 finished. Last value: 275263
5 finished. Last value: 282282
282282
Release
1 finished. Last value: 112543
2 finished. Last value: 269644
3 finished. Last value: 283430
4 finished. Last value: 322817
5 finished. Last value: 366932
366932
Example 3:
Debug
1 finished. Last value: 232918
4 finished. Last value: 239949
5 finished. Last value: 240443
2 finished. Last value: 242502
3 finished. Last value: 242681
242681
Release:
1 finished. Last value: 153611
2 finished. Last value: 165238
4 finished. Last value: 194810
3 finished. Last value: 204813
5 finished. Last value: 206372
206372
Example 4:
Debug
1 finished. Last value: 253266
2 finished. Last value: 263945
5 finished. Last value: 466285
4 finished. Last value: 478564
3 finished. Last value: 500000
500000
Release
5 finished. Last value: 173144
1 finished. Last value: 336224
4 finished. Last value: 354822
3 finished. Last value: 444017
2 finished. Last value: 500000
500000
use std::thread;
static mut LOCKED: bool = false;
static mut COUNTER: usize = 0;
pub fn test(inc: usize) -> usize {
while unsafe { LOCKED } {
}
unsafe { LOCKED = true };
unsafe { COUNTER += inc};
unsafe { LOCKED = false };
unsafe { COUNTER }
}
fn main() {
let t1 = thread::spawn(move || {
let mut prevent_optimization = Vec::with_capacity(100_000);
for _ in 0..100_000 {
let res = test(1);
prevent_optimization.push(res);
}
});
let mut prevent_optimization = Vec::with_capacity(100_000);
let t2 = thread::spawn(move || {
for _ in 0..100_000 {
let res = test(1);
prevent_optimization.push(res);
}
});
t1.join().unwrap();
t2.join().unwrap();
println!("{}", unsafe { COUNTER });
}
use std::thread;
use std::sync::atomic::{AtomicBool, Ordering};
static LOCKED: AtomicBool = AtomicBool::new(false);
static mut COUNTER: usize = 0;
#[inline(always)]
pub fn test(inc: usize) {
while LOCKED.load(Ordering::Relaxed) {
}
LOCKED.store(true, Ordering::Relaxed);
unsafe { COUNTER += inc};
LOCKED.store(false, Ordering::Relaxed);
}
fn main() {
let t1 = thread::spawn(move || {
for _ in 0..100_000 {
test(1);
}
});
let t2 = thread::spawn(move || {
for _ in 0..100_000 {
test(1);
}
});
t1.join().unwrap();
t2.join().unwrap();
println!("{}", unsafe { COUNTER });
}
use std::thread;
use std::sync::atomic::{AtomicBool, Ordering, fence};
static LOCKED: AtomicBool = AtomicBool::new(false);
static mut COUNTER: usize = 0;
#[inline(always)]
pub fn test(inc: usize) {
while LOCKED.compare_and_swap(false, true, Ordering::Acquire) {}
unsafe { COUNTER += inc };
LOCKED.store(false, Ordering::Release);
}
fn main() {
let t1 = thread::spawn(move || {
for _ in 0..100_000 {
test(1);
}
});
let t2 = thread::spawn(move || {
for _ in 0..100_000 {
test(1);
}
});
t1.join().unwrap();
t2.join().unwrap();
println!("{}", unsafe { COUNTER });
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment