rustHow to block a thread in Rust?
Threads can be blocked in Rust using the thread::park() function. This function will block the current thread until it is unparked.
use std::thread;
fn main() {
let handle = thread::spawn(|| {
println!("Thread is running");
});
thread::park();
println!("Thread is blocked");
}
Output example
Thread is running
Thread is blocked
The code above creates a new thread using thread::spawn() and then blocks it using thread::park(). The thread will remain blocked until it is unparked.
thread::spawn(): creates a new threadthread::park(): blocks the current thread
Helpful links
More of Rust
- How to use non-capturing groups in Rust regex?
- How to perform matrix operations in Rust?
- How to use regex lookbehind in Rust?
- How to convert a Rust slice of u8 to u32?
- How to sort a Rust HashMap?
- How to use regex lookahead in Rust?
- How to use regex to match a double quote in Rust?
- Regex example to match multiline string in Rust?
- How to sort the keys in a Rust HashMap?
- How to declare a constant Rust HashMap?
See more codes...