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 replace a capture group using Rust regex?
- Regex example to match multiline string in Rust?
- How to parse a file with Rust regex?
- How to use regex lookahead in Rust?
- How to use regex captures in Rust?
- How to use regex to match a group in Rust?
- How to match the end of a line in a Rust regex?
- How to perform matrix operations in Rust?
- How to use regex to match a double quote in Rust?
- How to replace strings using Rust regex?
See more codes...