Reader-writer locks v0.1.0
RwLock<T>
pub struct RwLock<T> {
// private fields
}
A value guarded by a lock that many readers or one writer may hold, shared between the threads that hold a handle.
Clone gives another handle to the same value. The value is destroyed when the last handle and the last guard are
gone. A handle is not copied.
Type parameters
| Name | Description |
|---|---|
T | the type of the guarded value |
Constructors
RwLock
pub func RwLock(value: T) -> RwLock<T>
A new lock guarding value. Running out of memory for it is a program error.
| Name | Type | Description |
|---|---|---|
value | T | the value to guard |
Returns: the first handle to it
Methods
Clone
pub func Clone(&self) -> RwLock<T>
Another handle to the same value.
Returns: the handle
Read
pub func Read(&self) -> ReadGuard<T>
Takes a shared hold, waiting while a writer holds the lock.
Returns: the read guard
Write
pub func Write(&self) -> WriteGuard<T>
Takes the exclusive hold, waiting while any reader or writer holds the lock.
Returns: the write guard
Destructor
~RwLock
func ~RwLock(&var self)
ReadGuard<T>
pub struct ReadGuard<T> {
// private fields
}
A shared hold on an RwLock<T>: the value may be read, and other readers may hold it too.
Type parameters
| Name | Description |
|---|---|
T | the type of the guarded value |
Methods
Get
pub func Get(&self) -> &T
The guarded value, borrowed for reading while the guard holds the lock. The guard stays borrowed while the reference is in use, so it cannot be dropped under it.
Returns: a reference to the value
Destructor
~ReadGuard
func ~ReadGuard(&var self)
WriteGuard<T>
pub struct WriteGuard<T> {
// private fields
}
The exclusive hold on an RwLock<T>: the value may be read and written, and nobody else holds the lock.
Type parameters
| Name | Description |
|---|---|
T | the type of the guarded value |
Methods
Get
pub func Get(&self) -> &T
The guarded value, borrowed for reading while the guard holds the lock. The guard stays borrowed while the reference is in use, so it cannot be dropped under it.
Returns: a reference to the value
GetMutable
pub func GetMutable(&var self) -> &var T
The guarded value, borrowed for writing, which this thread alone may do while the guard holds the lock. The guard stays borrowed exclusively while the reference is in use.
Returns: a writable reference to the value
Destructor
~WriteGuard
func ~WriteGuard(&var self)