Last active
September 14, 2016 18:36
-
-
Save Amanieu/9f4854c096fa519f046ace876779be60 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
extern crate crossbeam; | |
extern crate fringe; | |
use fringe::generator::*; | |
use fringe::OsStack; | |
use std::ptr; | |
use std::thread; | |
fn do_stuff(x: &mut i32) { | |
loop { | |
unsafe { | |
ptr::write_volatile(x, 1); | |
} | |
} | |
} | |
fn gen(yielder: &mut Yielder<(), ()>, input: ()) { | |
let mut var = 0; | |
crossbeam::scope(|scope| { | |
scope.spawn(|| do_stuff(&mut var)); | |
thread::sleep_ms(100); | |
yielder.suspend(()); | |
}); | |
} | |
fn main() { | |
let stack = OsStack::new(1<<16).unwrap(); | |
let mut g = Generator::new(stack, gen); | |
g.resume(()); | |
drop(g); | |
thread::sleep_ms(1000); | |
// Drop g here while another thread is still accessing var | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment