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 match a URL with a regex in Rust?
- How to replace a capture group using Rust regex?
- How to clear a Rust HashMap?
- Yield example in Rust
- Example of yield_now in Rust?
- How to convert a Rust slice to a fixed array?
- How to use regex to match a group in Rust?
- How to convert a Rust HashMap to a BTreeMap?
- How to yield a thread in Rust?
- How to replace strings using Rust regex?
See more codes...