Lock-free, thread-safe atomic versions of Duration, SystemTime, Instant and their Option variants
English | 简体中文
Introduction
atomic-time provides lock-free, thread-safe atomic versions of Rust's standard time types. All types use AtomicU128 (via portable-atomic) under the hood and expose the same API patterns as the standard std::sync::atomic types (load, store, swap, compare_exchange, compare_exchange_weak, fetch_update).
Types
| Type | Wraps | no_std |
|---|---|---|
AtomicDuration |
Duration |
Yes |
AtomicOptionDuration |
Option<Duration> |
Yes |
AtomicSystemTime |
SystemTime |
No |
AtomicOptionSystemTime |
Option<SystemTime> |
No |
AtomicInstant |
Instant |
No |
AtomicOptionInstant |
Option<Instant> |
No |
Installation
[]
= "0.2"
Feature Flags
| Feature | Default | Description |
|---|---|---|
std |
Yes | Enables SystemTime and Instant types |
serde |
No | Enables Serialize/Deserialize for all types |
For no_std environments (only AtomicDuration and AtomicOptionDuration are available):
[]
= { = "0.2", = false }
Example
use Arc;
use Ordering;
use Duration;
use AtomicDuration;
let timeout = new;
// Update from another thread
let timeout_clone = timeout.clone;
spawn;
use Ordering;
use Instant;
use AtomicOptionInstant;
// Track the last time an event occurred
let last_event = none;
assert_eq!;
last_event.store;
assert!;
Benchmarks
Run with cargo bench in the benchmark/ directory. Apple M4 Pro.
Duration (cargo bench --bench duration)
| Implementation | Single-thread load | Single-thread store | Contended read | Contended write |
|---|---|---|---|---|
AtomicDuration |
1.32 ns | 0.99 ns | 1.34 ns | 6.86 ns |
AtomicOptionDuration |
1.25 ns | 1.24 ns | 1.29 ns | 5.75 ns |
ArcSwap<Duration> |
2.35 ns | 93.63 ns | 2.37 ns | 11.73 ns |
parking_lot::RwLock |
3.62 ns | 2.20 ns | 46.92 ns | 218.22 ns |
std::sync::RwLock |
4.74 ns | 2.36 ns | 436.14 ns | 118.22 ns |
Instant (cargo bench --bench instant)
| Implementation | Single-thread load | Single-thread store | Contended read | Contended write |
|---|---|---|---|---|
AtomicInstant |
2.99 ns | 3.99 ns | 3.12 ns | 14.82 ns |
AtomicOptionInstant |
3.30 ns | 4.23 ns | 3.59 ns | 16.97 ns |
ArcSwap<Instant> |
2.30 ns | 85.67 ns | 2.40 ns | 15.51 ns |
parking_lot::RwLock |
3.42 ns | 2.13 ns | 8.70 ns | 197.00 ns |
std::sync::RwLock |
4.54 ns | 2.32 ns | 482.79 ns | 90.49 ns |
SystemTime (cargo bench --bench system_time)
| Implementation | Single-thread load | Single-thread store | Contended read | Contended write |
|---|---|---|---|---|
AtomicSystemTime |
2.19 ns | 3.56 ns | 2.32 ns | 10.58 ns |
AtomicOptionSystemTime |
2.55 ns | 3.27 ns | 2.68 ns | 11.08 ns |
ArcSwap<SystemTime> |
2.30 ns | 88.93 ns | 2.41 ns | 17.49 ns |
parking_lot::RwLock |
3.47 ns | 2.13 ns | 31.16 ns | 235.82 ns |
std::sync::RwLock |
4.52 ns | 2.31 ns | 561.88 ns | 106.58 ns |
Contended = 4 background threads reading (contended read) or writing (contended write), load measured on main thread.
MSRV
The minimum supported Rust version is 1.70.0.
License
atomic-time is under the terms of both the MIT license and the
Apache License (Version 2.0).
See LICENSE-APACHE, LICENSE-MIT for details.
Copyright (c) 2026 Al Liu.