Skip to content

Instantly share code, notes, and snippets.

@Amanieu
Last active September 14, 2016 18:36
Show Gist options
  • Save Amanieu/9f4854c096fa519f046ace876779be60 to your computer and use it in GitHub Desktop.
Save Amanieu/9f4854c096fa519f046ace876779be60 to your computer and use it in GitHub Desktop.
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