MRPT  2.0.4
thread_name_unittest.cpp
Go to the documentation of this file.
1 /* +------------------------------------------------------------------------+
2  | Mobile Robot Programming Toolkit (MRPT) |
3  | https://www.mrpt.org/ |
4  | |
5  | Copyright (c) 2005-2020, Individual contributors, see AUTHORS file |
6  | See: https://www.mrpt.org/Authors - All rights reserved. |
7  | Released under BSD License. See: https://www.mrpt.org/License |
8  +------------------------------------------------------------------------+ */
9 
10 #include <gtest/gtest.h>
11 #include <mrpt/config.h>
12 #include <mrpt/core/format.h>
14 
15 #include <atomic>
16 #include <condition_variable>
17 
18 // This functionality is not implemented in OSX
19 #if !defined(MRPT_OS_APPLE)
20 
21 TEST(thread_name, set_get_current_thread)
22 {
23  const std::string testValue = "CoolName";
24  const std::string testValue2 = "HotName";
25  // Set:
26  mrpt::system::thread_name(testValue);
27  // Get:
28  const std::string ret = mrpt::system::thread_name();
29  EXPECT_EQ(ret, testValue);
30 
31  // Set:
32  mrpt::system::thread_name(testValue2);
33  // Get:
34  const std::string ret2 = mrpt::system::thread_name();
35  EXPECT_EQ(ret2, testValue2);
36 }
37 
38 static std::condition_variable cv, cv2;
39 static std::mutex cv_m, cv2_m;
40 static std::atomic<int> thrCnt = 0;
41 static std::atomic<int> thrDone = 0;
42 
43 static const std::string sNewName = "NewName";
44 
45 static void testerThread(const std::string& myName)
46 {
47  // Notify we started:
48  {
49  std::lock_guard<std::mutex> lk(cv2_m);
50  thrCnt++;
51  cv2.notify_one();
52  }
53 
54  // wait for synchro point before checking name:
55  {
56  std::unique_lock<std::mutex> lk(cv_m);
57  cv.wait(lk, [&]() { return thrCnt == 0; });
58  }
59 
60  // check name matches:
62 
63  // change name:
65 
66  // signal:
67  {
68  std::lock_guard<std::mutex> lk(cv2_m);
69  thrDone++;
70  cv2.notify_one();
71  }
72 
73  // wait for final signal
74  {
75  std::unique_lock<std::mutex> lk(cv_m);
76  cv.wait(lk, [&]() { return thrDone == 0; });
77  }
78 }
79 
80 TEST(thread_name, set_get_other_thread)
81 {
82  constexpr unsigned int N = 10;
83  std::vector<std::thread> threads;
84  std::vector<std::string> names;
85 
86  for (unsigned int i = 0; i < N; i++)
87  {
88  const std::string thName = mrpt::format("tester_%u", i);
89  std::thread& t = threads.emplace_back(&testerThread, thName);
90  names.push_back(thName);
91 
92  mrpt::system::thread_name(thName, t);
93  }
94 
95  // Wait for all threads to start:
96  {
97  std::unique_lock<std::mutex> lk(cv2_m);
98  cv2.wait(lk, [&] { return thrCnt == N; });
99  }
100 
101  // Signal all that they can check their names:
102  {
103  std::lock_guard<std::mutex> lk(cv_m);
104  thrCnt = 0;
105  cv.notify_all();
106  }
107 
108  {
109  std::unique_lock<std::mutex> lk(cv2_m);
110  cv2.wait(lk, [&] { return thrDone == N; });
111  }
112 
113  for (auto& t : threads)
114  {
116  }
117 
118  {
119  std::lock_guard<std::mutex> lk(cv_m);
120  thrDone = 0;
121  cv.notify_all();
122  }
123 
124  for (auto& t : threads)
125  if (t.joinable()) t.join();
126 }
127 
128 #endif
std::string std::string format(std::string_view fmt, ARGS &&... args)
Definition: format.h:26
static std::condition_variable cv2
static std::atomic< int > thrCnt
Definition: img/CImage.h:23
void thread_name(const std::string &name, std::thread &theThread)
Sets the name of the given thread; useful for debuggers.
Definition: thread_name.cpp:63
TEST(thread_name, set_get_current_thread)
static std::atomic< int > thrDone
std::vector< string > names
static std::mutex cv_m
static std::condition_variable cv
EXPECT_EQ(out.image_pair_was_used.size(), NUM_IMGS)
static std::mutex cv2_m
static const std::string sNewName
static void testerThread(const std::string &myName)



Page generated by Doxygen 1.8.14 for MRPT 2.0.4 Git: 33de1d0ad Sat Jun 20 11:02:42 2020 +0200 at sáb jun 20 17:35:17 CEST 2020