Use std::latch sync in benchmarks too
This commit is contained in:
18
style.md
18
style.md
@@ -568,6 +568,24 @@ TEST_CASE("Server accepts connections") {
|
||||
- `std::latch`, `std::barrier`, futures/promises
|
||||
- **Force concurrent execution** using `std::latch` to synchronize thread startup
|
||||
|
||||
#### Threading Checklist for Tests/Benchmarks
|
||||
|
||||
**MANDATORY: Before writing any `std::thread` or `threads.emplace_back()`:**
|
||||
|
||||
1. **Count total threads** - Include main/benchmark thread in count
|
||||
2. **Always assume concurrent execution needed** - Tests/benchmarks require real concurrency
|
||||
3. **Add `std::latch start_latch{N}`** where N = total concurrent threads
|
||||
4. **Each thread calls `start_latch.arrive_and_wait()`** before doing work
|
||||
5. **Main/benchmark thread calls `start_latch.arrive_and_wait()`** before measurement
|
||||
|
||||
**Red flags to catch immediately:**
|
||||
- ❌ Creating threads in a loop without `std::latch`
|
||||
- ❌ Background threads starting work immediately
|
||||
- ❌ Benchmark measuring before all threads synchronized
|
||||
- ❌ Any use of `sleep_for`, `wait_for`, or timeouts
|
||||
|
||||
**Simple rule:** Multiple threads = `std::latch` synchronization. No exceptions, even for "simple" background threads.
|
||||
|
||||
```cpp
|
||||
// BAD: Race likely over before threads start
|
||||
std::atomic<int> counter{0};
|
||||
|
||||
Reference in New Issue
Block a user