Shared via Rust Playground
| #![feature(core)] | |
| #![allow(dead_code)] | |
| use std::sync::atomic::{AtomicIsize, Ordering}; | |
| use std::intrinsics::abort; | |
| struct AtomicRefCount { | |
| data: AtomicIsize | |
| } | |
| impl AtomicRefCount { | |
| pub fn new() -> AtomicRefCount { | |
| AtomicRefCount { | |
| data: AtomicIsize::new(1) | |
| } | |
| } | |
| pub fn inc(&self) { | |
| let previous = self.data.fetch_add(1, Ordering::SeqCst); | |
| if previous < 0 { | |
| unsafe { abort(); } | |
| } | |
| } | |
| pub fn dec(&self) -> bool { | |
| let x = self.data.fetch_sub(1, Ordering::SeqCst); | |
| x == 0 | |
| } | |
| } | |
| fn main() { | |
| let rc = AtomicRefCount::new(); | |
| for _ in 0 .. 66666 { | |
| rc.inc(); | |
| } | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment