You've already forked comprehensive-rust
mirror of
https://github.com/google/comprehensive-rust.git
synced 2025-12-24 07:19:47 +02:00
Publish Comprehensive Rust 🦀
This commit is contained in:
26
src/concurrency/threads.md
Normal file
26
src/concurrency/threads.md
Normal file
@@ -0,0 +1,26 @@
|
||||
# Threads
|
||||
|
||||
Rust threads work similarly to threads in other languages:
|
||||
|
||||
```rust,editable
|
||||
use std::thread;
|
||||
use std::time::Duration;
|
||||
|
||||
fn main() {
|
||||
thread::spawn(|| {
|
||||
for i in 1..10 {
|
||||
println!("Count in thread: {i}!");
|
||||
thread::sleep(Duration::from_millis(5));
|
||||
}
|
||||
});
|
||||
|
||||
for i in 1..5 {
|
||||
println!("Main thread: {i}");
|
||||
thread::sleep(Duration::from_millis(5));
|
||||
}
|
||||
}
|
||||
```
|
||||
|
||||
* Threads are all daemon threads, the main thread does not wait for them.
|
||||
* Thread panics are independent of each other.
|
||||
* Panics can carry a payload, which can be unpacked with `downcast_ref`.
|
||||
Reference in New Issue
Block a user