Initialize atomics in metrics, update style guide on atomics

This commit is contained in:
2025-08-29 10:52:26 -04:00
parent 1133d1e365
commit b6d4ae2862
3 changed files with 31 additions and 3 deletions

View File

@@ -301,6 +301,27 @@ for (auto &precondition : preconditions_) {
}
```
### Atomic Operations
- **Never use assignment operators** with `std::atomic` - always use explicit `store()` and `load()`
- **Always specify memory ordering** explicitly for atomic operations
- **Use the least restrictive correct memory ordering** - choose the weakest ordering that maintains correctness
```cpp
// Preferred - explicit store/load with precise memory ordering
std::atomic<uint64_t> counter;
counter.store(42, std::memory_order_relaxed); // Single-writer metric updates
auto value = counter.load(std::memory_order_relaxed); // Reading metrics for display
counter.store(1, std::memory_order_release); // Publishing initialization
auto ready = counter.load(std::memory_order_acquire); // Synchronizing with publisher
counter.store(42, std::memory_order_seq_cst); // When sequential consistency needed
// Avoid - assignment operators (implicit memory ordering)
std::atomic<uint64_t> counter;
counter = 42; // Implicit - memory ordering not explicit
auto value = counter; // Implicit - memory ordering not explicit
```
---
## Memory Management