Crates.io | batch-lock |
lib.rs | batch-lock |
version | 0.1.2 |
source | src |
created_at | 2024-11-19 09:16:58.294658 |
updated_at | 2024-11-21 07:01:22.61014 |
description | A lock manager with batch-lock support. |
homepage | https://github.com/SF-Zhou/batch-lock |
repository | https://github.com/SF-Zhou/batch-lock |
max_upload_size | |
id | 1453046 |
size | 35,952 |
A thread-safe, key-based lock management system for Rust that provides fine-grained concurrent access control.
dashmap
Add this to your Cargo.toml
:
[dependencies]
lock_manager = "0.1"
use batch_lock::LockManager;
// Create a new lock manager
let lock_manager = LockManager::<String>::new();
// Acquire a lock
let guard = lock_manager.lock("resource_1".to_string());
// Critical section - exclusive access guaranteed
// perform operations...
// Lock is automatically released when guard is dropped
use batch_lock::LockManager;
use std::collections::BTreeSet;
// Create a new lock manager with custom capacity
let lock_manager = LockManager::<String>::with_capacity(1000);
// Prepare multiple keys
let mut keys = BTreeSet::new();
keys.insert("resource_1".to_string());
keys.insert("resource_2".to_string());
// Acquire locks for all keys atomically
let guard = lock_manager.batch_lock(keys);
// Critical section - exclusive access to all keys guaranteed
// perform operations...
// All locks are automatically released when guard is dropped
use batch_lock::LockManager;
// Create a lock manager with custom capacity and shard count
let lock_manager = LockManager::<String>::with_capacity_and_shard_amount(
1000, // capacity
16 // number of shards
);
dashmap
for efficient concurrent accessThe LockManager
is fully thread-safe and can be safely shared across threads:
use std::sync::Arc;
use batch_lock::LockManager;
let lock_manager = Arc::new(LockManager::<String>::new());
let lock_manager_clone = lock_manager.clone();
std::thread::spawn(move || {
let guard = lock_manager_clone.lock("shared_resource".to_string());
// Critical section
});
For detailed API documentation, please visit docs.rs/batch-lock.
Contributions are welcome! Please feel free to submit a Pull Request.
This project is licensed under MIT or Apache-2.0.