mirror of
https://github.com/catchorg/Catch2.git
synced 2025-01-22 16:53:30 +01:00
Rewrite multithreading test to not use jthreads
This commit is contained in:
parent
29e64e4f22
commit
f1cb615f4e
@ -481,7 +481,7 @@ set_tests_properties(
|
|||||||
|
|
||||||
add_executable(Multithreading ${TESTS_DIR}/X37-Multithreading.cpp)
|
add_executable(Multithreading ${TESTS_DIR}/X37-Multithreading.cpp)
|
||||||
target_link_libraries(Multithreading PRIVATE Catch2::Catch2WithMain)
|
target_link_libraries(Multithreading PRIVATE Catch2::Catch2WithMain)
|
||||||
target_compile_features(Multithreading PRIVATE cxx_std_20)
|
target_compile_features(Multithreading PRIVATE cxx_std_11)
|
||||||
add_test(
|
add_test(
|
||||||
NAME Reporters::Multithreading
|
NAME Reporters::Multithreading
|
||||||
COMMAND ${CMAKE_COMMAND} -E env $<TARGET_FILE:Multithreading>
|
COMMAND ${CMAKE_COMMAND} -E env $<TARGET_FILE:Multithreading>
|
||||||
|
@ -11,25 +11,27 @@
|
|||||||
#include <catch2/benchmark/catch_benchmark.hpp>
|
#include <catch2/benchmark/catch_benchmark.hpp>
|
||||||
|
|
||||||
#include <thread>
|
#include <thread>
|
||||||
#include <stop_token>
|
#include <atomic>
|
||||||
|
|
||||||
TEST_CASE( "ThreadAssertionTest",
|
TEST_CASE( "ThreadAssertionTest",
|
||||||
"[Multithreading]" ) {
|
"[Multithreading]" ) {
|
||||||
|
std::atomic_bool should_stop = false;
|
||||||
SECTION( "Basic" ) {
|
SECTION( "Basic" ) {
|
||||||
std::jthread a([] (const std::stop_token& token) {
|
std::thread a([&should_stop] () {
|
||||||
while (!token.stop_requested()) {
|
while (!should_stop) {
|
||||||
FAIL_CHECK(false);
|
FAIL_CHECK(false);
|
||||||
CHECK(true);
|
CHECK(true);
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
std::jthread b([] (const std::stop_token& token) {
|
std::thread b([&should_stop] () {
|
||||||
while (!token.stop_requested()) {
|
while (!should_stop) {
|
||||||
FAIL_CHECK(false);
|
FAIL_CHECK(false);
|
||||||
CHECK(true);
|
CHECK(true);
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
std::this_thread::sleep_for( std::chrono::milliseconds( 1'000 ) );
|
std::this_thread::sleep_for( std::chrono::milliseconds( 1'000 ) );
|
||||||
a.get_stop_source().request_stop();
|
should_stop = true;
|
||||||
b.get_stop_source().request_stop();
|
a.join();
|
||||||
|
b.join();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user