]> git.lizzy.rs Git - rust.git/commitdiff
add scoped thread test
authorRalf Jung <post@ralfj.de>
Sun, 27 Nov 2022 11:57:30 +0000 (12:57 +0100)
committerRalf Jung <post@ralfj.de>
Mon, 28 Nov 2022 07:53:14 +0000 (08:53 +0100)
src/tools/miri/tests/pass/concurrency/scope.rs [new file with mode: 0644]

diff --git a/src/tools/miri/tests/pass/concurrency/scope.rs b/src/tools/miri/tests/pass/concurrency/scope.rs
new file mode 100644 (file)
index 0000000..ce5d17f
--- /dev/null
@@ -0,0 +1,24 @@
+use std::thread;
+
+fn main() {
+    let mut a = vec![1, 2, 3];
+    let mut x = 0;
+
+    thread::scope(|s| {
+        s.spawn(|| {
+            // We can borrow `a` here.
+            let _s = format!("hello from the first scoped thread: {a:?}");
+        });
+        s.spawn(|| {
+            let _s = format!("hello from the second scoped thread");
+            // We can even mutably borrow `x` here,
+            // because no other threads are using it.
+            x += a[0] + a[2];
+        });
+        let _s = format!("hello from the main thread");
+    });
+
+    // After the scope, we can modify and access our variables again:
+    a.push(4);
+    assert_eq!(x, a.len());
+}