Locker Room
Readers-writer access to individual cells of your collection!
LockerRoom
The central feature of the crate is implemented by this structure. More specifically, it provides such functionality:
- Shared readers access to single cell of collection with
read_cellmethod; - Exclusive writer access to single cell of collection with
write_cellmethod; - Exclusive writer access to whole collection with
lock_roommethod.
Example
let v = vec!;
let locker_room: = v.into;
let locker_room = new;
scope;
assert_eq!;
Deadlock example
Carefully block multiple cells in one scope. Otherwise, situation like this may occur:
// Thread 1 | // Thread 2
let _w1 = locker_room.write_cell(0); |
| let _w1 = locker_room.write_cell(1);
// will block
let _w2 = locker_room.write_cell(1); |
| // will deadlock
| let _w2 = locker_room.write_cell(0);
Collections?
By default you can create LockerRoom from array, Vec, VecDeque, HashMap and BTreeMap.
But the crate provides trait, by which implementing to your collection, you can make it compatible with LockerRoom.
Collection
Crucial part of the crate that helps your collection to be compatible with LockerRoom.
Just implement it into your collection and everything will work!
Example
Let's implement the trait for the struct from Index's example: