thread-counter 0.1.1

A simple way of efficiently keeping track of active threads and waiting for them to exit.
Documentation
  • Coverage
  • 100%
    8 out of 8 items documented1 out of 8 items with examples
  • Size
  • Source code size: 29.27 kB This is the summed size of all the files inside the crates.io package for this release.
  • Documentation size: 1.98 MB This is the summed size of all files generated by rustdoc for all configured targets
  • Ø build duration
  • this release: 12s Average build duration of successful builds.
  • all releases: 12s Average build duration of successful builds in releases after 2024-10-23.
  • Links
  • Absolucy/thread-counter-rs
    0 0 0
  • crates.io
  • Dependencies
  • Versions
  • Owners
  • Absolucy

Crates.io Version Crates.io License Crates.io Downloads (recent) Crates.io Size Maintenance

thread-counter

A lightweight, thread-safe library for counting and synchronizing concurrent operations.

This crate provides a ThreadCounter type that can be used to keep track of the number of active threads or operations, and to synchronize the completion of these operations. It's particularly useful for scenarios where you need to wait for a group of tasks to complete before proceeding.

Features

  • Thread-safe counting of active operations.
  • RAII-based automatic decrementing using Tickets.
  • Ability to wait for all operations to complete, with optional timeout.

Usage

Here's a basic example of how to use the ThreadCounter:

use std::{thread, time::Duration};
use thread_counter::ThreadCounter;

let counter = ThreadCounter::default();

// Spawn some threads
for _ in 0..5 {
	thread::spawn(move || {
		// Take a ticket, incrementing the counter.
		let ticket = counter.ticket();
		// Simulate some work
		thread::sleep(Duration::from_millis(100));
		// `ticket` is automatically dropped here, decrementing the counter
	});
}

// Wait for all threads to complete, timing out after 200ms.
counter.wait(Duration::from_millis(200));
println!("All threads have completed!");

Current version: 0.1.0

Some additional info here

License

thread-counter is dual-licensed under the MIT license and the Apache License (Version 2.0).