rippled
Loading...
Searching...
No Matches
beast_CurrentThreadName_test.cpp
1#include <xrpl/beast/core/CurrentThreadName.h>
2#include <xrpl/beast/unit_test.h>
3
4#include <thread>
5
6namespace ripple {
7namespace test {
8
10{
11private:
12 static void
14 std::string myName,
16 std::atomic<int>* state)
17 {
18 // Verify that upon creation a thread has no name.
19 auto const initialThreadName = beast::getCurrentThreadName();
20
21 // Set the new name.
23
24 // Indicate to caller that the name is set.
25 *state = 1;
26
27 // If there is an initial thread name then we failed.
28 if (!initialThreadName.empty())
29 return;
30
31 // Wait until all threads have their names.
32 while (!*stop)
33 ;
34
35 // Make sure the thread name that we set before is still there
36 // (not overwritten by, for instance, another thread).
37 if (beast::getCurrentThreadName() == myName)
38 *state = 2;
39 }
40
41public:
42 void
43 run() override
44 {
45 // Make two different threads with two different names. Make sure
46 // that the expected thread names are still there when the thread
47 // exits.
48 std::atomic<bool> stop{false};
49
50 std::atomic<int> stateA{0};
51 std::thread tA(exerciseName, "tA", &stop, &stateA);
52
53 std::atomic<int> stateB{0};
54 std::thread tB(exerciseName, "tB", &stop, &stateB);
55
56 // Wait until both threads have set their names.
57 while (stateA == 0 || stateB == 0)
58 ;
59
60 stop = true;
61 tA.join();
62 tB.join();
63
64 // Both threads should still have the expected name when they exit.
65 BEAST_EXPECT(stateA == 2);
66 BEAST_EXPECT(stateB == 2);
67 }
68};
69
70BEAST_DEFINE_TESTSUITE(CurrentThreadName, beast, beast);
71
72} // namespace test
73} // namespace ripple
A testsuite class.
Definition suite.h:52
static void exerciseName(std::string myName, std::atomic< bool > *stop, std::atomic< int > *state)
T join(T... args)
void setCurrentThreadName(std::string_view newThreadName)
Changes the name of the caller thread.
std::string getCurrentThreadName()
Returns the name of the caller thread.
Use hash_* containers for keys that do not need a cryptographically secure hashing algorithm.
Definition algorithm.h:6