pub struct ReentrantLock { /* private fields */ }Expand description
ReentrantLock implemented using Semaphore(1).
This Lock does not need to be mutable on calling, it is safe to share across multiple threads even with read only mode.
Example:
use std::sync::Arc;
use std::time::Duration;
use std::thread;
use classic_sync::lock::ReentrantLock;
let lock = ReentrantLock::new();
let arc_lock = Arc::new(lock);
for i in 0..3 {
let lock_copy = Arc::clone(&arc_lock);
let tid = i;
thread::spawn(move || {
lock_copy.lock();
println!("Now we are in critical section!");
std::thread::sleep(Duration::from_secs(3));
// Other people can't acquire the lock even when I am sleeping.
lock_copy.unlock(); // You have to manually unlock it to release the lock
});
}Implementations§
Source§impl ReentrantLock
Implementation of ReentrantLock
impl ReentrantLock
Implementation of ReentrantLock
Sourcepub fn new() -> ReentrantLock
pub fn new() -> ReentrantLock
Create a new Lock object. Lock can be shared using Arc
Sourcepub fn lock(&self)
pub fn lock(&self)
Acquire the lock and wait indefinitely for it to happen Calling on already locked lock will block forever (dead lock) It is calling internal semaphore’s p() operation effectively.
Auto Trait Implementations§
impl Freeze for ReentrantLock
impl RefUnwindSafe for ReentrantLock
impl Send for ReentrantLock
impl Sync for ReentrantLock
impl Unpin for ReentrantLock
impl UnwindSafe for ReentrantLock
Blanket Implementations§
Source§impl<T> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
Source§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
Mutably borrows from an owned value. Read more