[src]

Struct sync::RWLock

pub struct RWLock<T> {
    // some fields omitted
}

A dual-mode reader-writer lock. The data can be accessed mutably or immutably, and immutably-accessing tasks may run concurrently.

Example

use sync::{RWLock, Arc};

let lock1 = Arc::new(RWLock::new(1));
let lock2 = lock1.clone();

spawn(proc() {
    let mut val = lock2.write();
    *val = 3;
    let val = val.downgrade();
    println!("{}", *val);
});

let val = lock1.read();
println!("{}", *val);

Methods

impl<T: Send + Share> RWLock<T>

fn new(user_data: T) -> RWLock<T>

Create a reader/writer lock with the supplied data.

fn new_with_condvars(user_data: T, num_condvars: uint) -> RWLock<T>

Create a reader/writer lock with the supplied data and a specified number of condvars (as sync::RWLock::new_with_condvars).

fn write<'a>(&'a self) -> RWLockWriteGuard<'a, T>

Access the underlying data mutably. Locks the rwlock in write mode; other readers and writers will block.

Failure

Failing while inside the lock will unlock the lock while unwinding, so that other tasks won't block forever. As Mutex.lock, it will also poison the lock, so subsequent readers and writers will both also fail.

fn read<'a>(&'a self) -> RWLockReadGuard<'a, T>

Access the underlying data immutably. May run concurrently with other reading tasks.

Failure

Failing will unlock the lock while unwinding. However, unlike all other access modes, this will not poison the lock.