Make assertions thread-safe

This commit is contained in:
Jeremy Rifkin
2025-01-17 17:44:20 -06:00
parent 914aeecfe2
commit 83cbfb953a
14 changed files with 172 additions and 29 deletions

View File

@@ -58,23 +58,9 @@ again.
This section outlines some missing features, what is their status and their possible workarounds.
### Thread safe assertions
Catch2's assertion macros are not thread safe. This does not mean that
you cannot use threads inside Catch's test, but that only single thread
can interact with Catch's assertions and other macros.
Catch2's assertion macros and logging macros are thread safe.
This means that this is ok
```cpp
std::vector<std::thread> threads;
std::atomic<int> cnt{ 0 };
for (int i = 0; i < 4; ++i) {
threads.emplace_back([&]() {
++cnt; ++cnt; ++cnt; ++cnt;
});
}
for (auto& t : threads) { t.join(); }
REQUIRE(cnt == 16);
```
because only one thread passes the `REQUIRE` macro and this is not
This is ok however it was previously not ok for Catch2 3.8.0 and earlier:
```cpp
std::vector<std::thread> threads;
std::atomic<int> cnt{ 0 };
@@ -88,8 +74,6 @@ because only one thread passes the `REQUIRE` macro and this is not
REQUIRE(cnt == 16);
```
We currently do not plan to support thread-safe assertions.
### Process isolation in a test
Catch does not support running tests in isolated (forked) processes. While this might in the future, the fact that Windows does not support forking and only allows full-on process creation and the desire to keep code as similar as possible across platforms, mean that this is likely to take significant development time, that is not currently available.