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?
- How to use non-capturing groups in Rust regex?
- How to replace all matches using Rust regex?
- How to match the end of a line in a Rust regex?
- How to use Unicode in a regex in Rust?
- How to split a string with Rust regex?
- How to use regex to match a double quote in Rust?
- How to replace strings using Rust regex?
- How to match whitespace with a regex in Rust?
- How to convert Rust bytes to a string?
See more codes...