forked from chemicstry/wasm_thread
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsimple.rs
77 lines (62 loc) · 2.54 KB
/
simple.rs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
use std::time::Duration;
use wasm_thread as thread;
fn main() {
#[cfg(target_arch = "wasm32")]
{
console_log::init().unwrap();
console_error_panic_hook::set_once();
}
#[cfg(not(target_arch = "wasm32"))]
env_logger::init_from_env(env_logger::Env::default().filter_or(env_logger::DEFAULT_FILTER_ENV, "info"));
log::info!("Available parallelism: {:?}", thread::available_parallelism());
let mut threads = vec![];
for _ in 0..2 {
threads.push(thread::spawn(|| {
for i in 1..3 {
log::info!("hi number {} from the spawned thread {:?}!", i, thread::current().id());
thread::sleep(Duration::from_millis(1));
}
}));
}
for i in 1..3 {
log::info!("hi number {} from the main thread {:?}!", i, thread::current().id());
}
// It's not possible to do a scope on the main thread, because blocking waits are not supported, but we can use
// scope inside web workers.
threads.push(thread::spawn(|| {
log::info!("Start scope test on thread {:?}", thread::current().id());
let mut a = vec![1, 2, 3];
let mut x = 0;
thread::scope(|s| {
let handle = s.spawn(|| {
log::info!("hello from the first scoped thread {:?}", thread::current().id());
// We can borrow `a` here.
log::info!("a = {:?}", &a);
// Return a subslice of borrowed `a`
&a[0..2]
});
// Wait for the returned value from first thread
log::info!("a[0..2] = {:?}", handle.join().unwrap());
s.spawn(|| {
log::info!("hello from the second scoped thread {:?}", thread::current().id());
// We can even mutably borrow `x` here,
// because no other threads are using it.
x += a[0] + a[2];
});
log::info!(
"Hello from scope \"main\" thread {:?} inside scope.",
thread::current().id()
);
});
// After the scope, we can modify and access our variables again:
a.push(4);
assert_eq!(x, a.len());
log::info!("Scope done x = {}, a.len() = {}", x, a.len());
}));
// Wait for all threads, otherwise program exits before threads finish execution.
// We can't do blocking join on wasm main thread though, but the browser window will continue running.
#[cfg(not(target_arch = "wasm32"))]
for handle in threads {
handle.join().unwrap();
}
}