Main Page
Related Pages
Modules
Namespaces
Classes
Files
Examples
File List
File Members
cetlib
cetlib
test
assert_only_one_thread_test.cc
Go to the documentation of this file.
1
#include "
cetlib/SimultaneousFunctionSpawner.h
"
2
#include "
cetlib/assert_only_one_thread.h
"
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
27
main
()
28
{
29
CET_ASSERT_ONLY_ONE_THREAD
();
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__};
34
cet::detail::ThreadCounter::Sentry
hold{
s
,
false
};
35
std::this_thread::sleep_for(100
ms
);
36
};
37
SimultaneousFunctionSpawner
launch{
repeated_task
(10u, will_fail)};
38
}
assert_only_one_thread.h
CET_ASSERT_ONLY_ONE_THREAD
#define CET_ASSERT_ONLY_ONE_THREAD()
Definition:
assert_only_one_thread.h:70
chrono_literals
main
int main()
Definition:
assert_only_one_thread_test.cc:27
cet
Definition:
get_LibraryInfoCollection.h:10
cet::SimultaneousFunctionSpawner
Definition:
SimultaneousFunctionSpawner.h:47
SimultaneousFunctionSpawner.h
cet::detail::ThreadCounter::Sentry
Definition:
assert_only_one_thread.h:38
cet::repeated_task
auto repeated_task(std::size_t const nInstances, F func)
Definition:
SimultaneousFunctionSpawner.h:42
genie::units::ms
static const double ms
Definition:
Units.h:99
s
static QCString * s
Definition:
config.cpp:1042
cet::detail::ThreadCounter
Definition:
assert_only_one_thread.h:30
Generated by
1.8.11