Initialize atomics in metrics, update style guide on atomics
This commit is contained in:
21
style.md
21
style.md
@@ -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
|
||||
|
||||
Reference in New Issue
Block a user