mirror of
https://github.com/metabarcoding/obitools4.git
synced 2026-04-30 12:00:39 +00:00
8c7017a99d
- Update obioptions.Version from "Release 4.4.29" to "/v/ Release v5" - Update version.txt from 4.29 → .30 (automated by Makefile)
1.1 KiB
1.1 KiB
obiutils.Counter: Thread-Safe Atomic Counter
A minimal, thread-safe counter implementation in Go.
Features
- Atomic increment/decrement:
Inc()andDec()modify the internal counter atomically using a mutex. - Current value retrieval:
Value()safely returns the current count without modifying it. - Initial value support: Constructor accepts an optional initial integer (defaults to
0). - Closure-based API: Encapsulates state and synchronization behind clean, functional methods.
- No external dependencies: Uses only the standard library (
sync).
Usage Example
counter := obiutils.NewCounter(10) // start at 10
fmt.Println(counter.Inc()) // → 11
fmt.Println(counter.Dec()) // → 10
fmt.Println(counter.Value()) // → 10 (unchanged)
Thread Safety
All operations are protected by a sync.Mutex, ensuring correctness in concurrent environments.
Design Notes
- Immutable interface: methods return updated values, not pointers.
- No reset method provided—intentionally minimal and focused on core counting semantics.