range-lock - Multithread range lock for Vec
This crate provides locks/mutexes for multi-threaded access to a single Vec<T> instance.
Any thread can request exclusive access to a slice of the Vec.
Such access is granted, if no other thread is simultaneously holding the permission to access an overlapping slice.
Usage
Add this to your Cargo.toml:
[]
= "0.2"
VecRangeLock example usage
General purpose VecRangeLock:
use VecRangeLock;
use Arc;
use thread;
let lock = new;
spawn;
RepVecRangeLock example usage
The RepVecRangeLock is a restricted range lock, that provides access to interleaved patterns of slices to the threads.
Locking a RepVecRangeLock is more lightweight than locking a VecRangeLock.
The threads can not freely choose slice ranges, but only choose a repeating slice pattern by specifying a pattern offset.
Please see the example below.
use RepVecRangeLock;
use Arc;
use thread;
let data = vec!; // <- cycle 1
// ^ ^ ^
// | | |
// | | offset-2
// offset-0 offset-1
let lock = new; // cycle_len: Each cycle has 3 slices (offsets).
spawn;
TODOs for future releases
The following new features might be candidates for future releases:
- Sleeping lock, in case of lock contention.
- Add support for arrays.
License
Copyright (c) 2021-2025 Michael Büsch m@bues.ch
Licensed under the Apache License version 2.0 or the MIT license, at your option.