Add "force concurrent execution" to style guide
This commit is contained in:
21
style.md
21
style.md
@@ -417,6 +417,27 @@ TEST_CASE("Server accepts connections") {
|
|||||||
- `std::latch`, `std::barrier`, futures/promises
|
- `std::latch`, `std::barrier`, futures/promises
|
||||||
- RAII guards and resource management
|
- RAII guards and resource management
|
||||||
|
|
||||||
|
### Multithreading Test Correctness
|
||||||
|
- **Force concurrent execution** - Thread creation takes time, so work often completes sequentially before threads start
|
||||||
|
- **Use std::latch to synchronize thread startup** - Ensures all threads begin racing simultaneously
|
||||||
|
```cpp
|
||||||
|
// BAD: Race likely over before threads start
|
||||||
|
std::atomic<int> counter{0};
|
||||||
|
for (int i = 0; i < 4; ++i) {
|
||||||
|
threads.emplace_back([&]() { counter++; }); // Probably sequential
|
||||||
|
}
|
||||||
|
|
||||||
|
// GOOD: Force threads to race simultaneously
|
||||||
|
std::atomic<int> counter{0};
|
||||||
|
std::latch start_latch{4};
|
||||||
|
for (int i = 0; i < 4; ++i) {
|
||||||
|
threads.emplace_back([&]() {
|
||||||
|
start_latch.count_down_and_wait(); // All threads start together
|
||||||
|
counter++; // Now they actually race
|
||||||
|
});
|
||||||
|
}
|
||||||
|
```
|
||||||
|
|
||||||
---
|
---
|
||||||
|
|
||||||
## Build Integration
|
## Build Integration
|
||||||
|
|||||||
Reference in New Issue
Block a user