about summary refs log tree commit diff
path: root/src/tools/miri/tests/pass/concurrency/simple.rs
blob: 46033eea35d1de8476b61fbf019e0cd81647538a (plain)
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
//@compile-flags: -Zmiri-strict-provenance

use std::thread;

fn create_and_detach() {
    thread::spawn(|| ());
}

fn create_and_join() {
    thread::spawn(|| ()).join().unwrap();
}

fn create_and_get_result() {
    let nine = thread::spawn(|| 5 + 4).join().unwrap();
    assert_eq!(nine, 9);
}

fn create_and_leak_result() {
    thread::spawn(|| 7);
}

fn create_nested_and_detach() {
    thread::spawn(|| {
        thread::spawn(|| ());
    });
}

fn create_nested_and_join() {
    let handle = thread::spawn(|| thread::spawn(|| ()));
    let handle_nested = handle.join().unwrap();
    handle_nested.join().unwrap();
}

fn create_move_in() {
    let x = String::from("Hello!");
    thread::spawn(move || {
        assert_eq!(x.len(), 6);
    })
    .join()
    .unwrap();
}

fn create_move_out() {
    let result = thread::spawn(|| String::from("Hello!")).join().unwrap();
    assert_eq!(result.len(), 6);
}

// This is not a data race!
fn shared_readonly() {
    use std::sync::Arc;

    let x = Arc::new(42i32);
    let h = thread::spawn({
        let x = Arc::clone(&x);
        move || {
            assert_eq!(*x, 42);
        }
    });

    assert_eq!(*x, 42);

    h.join().unwrap();
}

fn main() {
    create_and_detach();
    create_and_join();
    create_and_get_result();
    create_and_leak_result();
    create_nested_and_detach();
    create_nested_and_join();
    create_move_in();
    create_move_out();
    shared_readonly();
}