Skip to content

Instantly share code, notes, and snippets.

@larsbergstrom
Last active December 25, 2015 06:38
Show Gist options
  • Save larsbergstrom/6933164 to your computer and use it in GitHub Desktop.
Save larsbergstrom/6933164 to your computer and use it in GitHub Desktop.
Test concurrency with start_on_main_thread
use std::comm;
#[start]
fn start(argc: int, argv: **u8, crate_map: *u8) -> int {
do std::rt::start_on_main_thread(argc, argv, crate_map) {
run()
}
}
fn run() {
let (port, chan) = comm::stream();
do spawn {
for i in range(0i, 10) {
println(fmt!("nested, %d", fib(i * 3)));
}
chan.send(());
};
for i in range(0i, 10) {
println(fmt!("toplevel, %d", fib(i * 4)));
}
println("waiting");
port.recv();
println("done");
}
fn fib(x: int) -> int { if x < 2 {x} else { fib(x - 1) + fib(x - 2) } }
@larsbergstrom
Copy link
Author

Output:
[lbergstrom@lbergstrom tmp]$ rustc test-concur.rs && ./test-concur
warning: no debug symbols in executable (-arch x86_64)
toplevel, 0nested, 0

nested, 2toplevel, 3

nested, 8
toplevel, 21
nested, 34
toplevel, 144
nested, 144
nested, 610
toplevel, 987
nested, 2584
toplevel, 6765
nested, 10946
toplevel, 46368
nested, 46368
nested, 196418
toplevel, 317811
toplevel, 2178309
toplevel, 14930352
waiting
done
[lbergstrom@lbergstrom tmp]$

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment