Skip to content
This repository was archived by the owner on May 28, 2025. It is now read-only.

Commit c9b9c17

Browse files
committed
add scoped thread test
1 parent ec003fd commit c9b9c17

File tree

1 file changed

+24
-0
lines changed
  • src/tools/miri/tests/pass/concurrency

1 file changed

+24
-0
lines changed
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
use std::thread;
2+
3+
fn main() {
4+
let mut a = vec![1, 2, 3];
5+
let mut x = 0;
6+
7+
thread::scope(|s| {
8+
s.spawn(|| {
9+
// We can borrow `a` here.
10+
let _s = format!("hello from the first scoped thread: {a:?}");
11+
});
12+
s.spawn(|| {
13+
let _s = format!("hello from the second scoped thread");
14+
// We can even mutably borrow `x` here,
15+
// because no other threads are using it.
16+
x += a[0] + a[2];
17+
});
18+
let _s = format!("hello from the main thread");
19+
});
20+
21+
// After the scope, we can modify and access our variables again:
22+
a.push(4);
23+
assert_eq!(x, a.len());
24+
}

0 commit comments

Comments
 (0)