summaryrefslogtreecommitdiff
path: root/examples/all_features/concurrency.cpp
blob: 844ebaa3ae3dd401d3ed7110fc1749a5347b6589 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
#include <doctest/doctest.h>

#ifndef DOCTEST_CONFIG_NO_EXCEPTIONS

DOCTEST_MAKE_STD_HEADERS_CLEAN_FROM_WARNINGS_ON_WALL_BEGIN
#include <thread>
#include <mutex>
#include <exception>
#include <stdexcept>
DOCTEST_MAKE_STD_HEADERS_CLEAN_FROM_WARNINGS_ON_WALL_END

DOCTEST_MSVC_SUPPRESS_WARNING(4626) // assignment operator was implicitly defined as deleted

TEST_CASE("threads...") {
    auto call_from_thread = [](int value) {
        INFO("print me!");
        // one of these has to fail
        CHECK(value == 1);
        CHECK(value == 2);
    };

    int data_1 = 1;
    int data_2 = 2;
    CAPTURE(data_1); // will not be used for assertions in other threads

    // subcases have to be used only in the main thread (where the test runner is)
    SUBCASE("test runner thread") {
        call_from_thread(data_1);
    }

    // normal threads which are assumed not to throw
    SUBCASE("spawned threads") {
        std::thread t1(call_from_thread, data_1);
        std::thread t2(call_from_thread, data_2);

        t1.join();
        t2.join();
    }

    // exceptions from threads (that includes failing REQUIRE asserts) have to be handled explicitly
    SUBCASE("spawned threads with exception propagation") {
        std::exception_ptr exception_ptr = nullptr;
        std::mutex         mutex;

        auto might_throw = [&]() {
            try {
                REQUIRE(1 == 1);
                REQUIRE(1 == 2); // will fail and throw an exception
                MESSAGE("not reached!");
            } catch(...) {
                // make sure there are no races when dealing with the exception ptr
                std::lock_guard<std::mutex> lock(mutex);

                // set the exception pointer in case of an exception - might overwrite
                // another exception but here we care about propagating any exception - not all
                exception_ptr = std::current_exception();
            }
        };
        std::thread t1(might_throw);
        std::thread t2(might_throw);

        t1.join();
        t2.join();

        // if any thread has thrown an exception - rethrow it
        if(exception_ptr)
            std::rethrow_exception(exception_ptr);
    }
}

#endif // DOCTEST_CONFIG_NO_EXCEPTIONS