【笔记】Rust的多线程

Preface

Notes on Rust multi-threading

Creating child threads in the main thread

  • Define functions through closures in child threads
  • When the main thread finishes running, other child threads will also terminate
1
2
3
4
5
fn main() {
thread::spawn(|| {
...
});
}

Waiting for child threads after the main thread finishes execution

1
2
3
4
5
6
fn main() {
let handler = thread::spawn(|| {
...
});
handler.join().unwrap();
}

Thread sleep

Duration::from_millis(<num>): In milliseconds

1
thread::sleep(Duration::from_millis(<num>));

Conclusion

References

Bilibili - Learning for a Pay Raise