| Crates.io | named-sem |
| lib.rs | named-sem |
| version | 0.2.3 |
| created_at | 2024-01-03 03:26:32.886481+00 |
| updated_at | 2025-07-13 01:13:29.614183+00 |
| description | Named semaphore for Linux & Windows. |
| homepage | https://github.com/Evian-Zhang/named-semaphore |
| repository | https://github.com/Evian-Zhang/named-semaphore |
| max_upload_size | |
| id | 1086969 |
| size | 45,051 |
Named semaphore for Linux & Windows.
To use this crate, add the following to Cargo.toml:
[dependencies]
named-sem = "0.2"
Named semaphore is a process synchronize mechanism provided by OS and libc in Linux & Windows. By giving the semaphore a name, processes with appropriate permissions can share the same semaphore.
In Linux, we use the POSIX semaphore as implementation, and in Windows, we use Semaphore Objects.
use named_sem::{NamedSemaphore, Error};
# fn do_heavy_things() {}
fn use_named_semaphore() -> Result<(), Error> {
// In Linux, the semaphore's name should begin with "/"
let mut semaphore = NamedSemaphore::create("/my-semaphore", 3)?;
semaphore.wait_then_post(|| {
do_heavy_things();
})?;
Ok(())
}
A common usage for named semaphore is to control the process count across the system.
For example, we have four large directories, A, B, C and D, which needs to be compressed. While our computer's hardware only allows for two 7z processes to run at the same time. So we can create a named semaphore with initial value 2, and require the semaphore before each 7z run.
There is a small utility, preempt-do, in this repo. Compile it by
cargo build --release --features=commandline --bin preempt-do
Or you can install it by
cargo install named-sem --features=commandline --bin preempt-do
Then you can use the following instructions to do the things above:
preempt-do --name /my-semaphore --count 2 -- 7z a A.7z A
preempt-do --name /my-semaphore --count 2 -- 7z a B.7z B
preempt-do --name /my-semaphore --count 2 -- 7z a C.7z C
preempt-do --name /my-semaphore --count 2 -- 7z a D.7z D
This will allow there to be no more than two 7z processes across the system.