assert_only_one_thread_test.cc
Go to the documentation of this file.
3 
4 #include <chrono>
5 #include <thread>
6 
7 using namespace cet;
8 using namespace std::chrono_literals;
9 
10 // This test is 'fragile' in that, on a heavily loaded system, we
11 // might see:
12 // 1. all threads hit the "barrier" in SimultaneousFunctionSpawner
13 // 2. all threads start executing
14 // 3. one thread swaps out before getting to increment the counter
15 // in the ThreadCounter.
16 // 4. the other thread completes (increments and then decrements
17 // the counter)
18 // 5. the sappwed thread comes back, and is not running at the
19 // same time as the other thread, so the expected violation
20 // is not seen.
21 
22 // We attempt to make this very rare by:
23 // running a function that takes a long time, and
24 // running many threads, hoping not all will swap out in
25 // the right order so that we see NO overlaps.
26 int
28 {
30  auto will_fail = [] {
31  // Insert "by-hand" what the CET_ASSERT_ONLY_ONE_THREAD would
32  // expand to so that we can avoid an std::abort() for testing.
33  static cet::detail::ThreadCounter s{__FILE__, __LINE__, __func__};
35  std::this_thread::sleep_for(100ms);
36  };
37  SimultaneousFunctionSpawner launch{repeated_task(10u, will_fail)};
38 }
#define CET_ASSERT_ONLY_ONE_THREAD()
auto repeated_task(std::size_t const nInstances, F func)
static const double ms
Definition: Units.h:99
static QCString * s
Definition: config.cpp:1042