Main MRPT website > C++ reference for MRPT 1.5.7
CCriticalSection_unittest.cpp
Go to the documentation of this file.
1 /* +---------------------------------------------------------------------------+
2  | Mobile Robot Programming Toolkit (MRPT) |
3  | http://www.mrpt.org/ |
4  | |
5  | Copyright (c) 2005-2017, Individual contributors, see AUTHORS file |
6  | See: http://www.mrpt.org/Authors - All rights reserved. |
7  | Released under BSD License. See details in http://www.mrpt.org/License |
8  +---------------------------------------------------------------------------+ */
9 
10 #include <mrpt/synch.h>
11 #include <mrpt/utils/CTicTac.h>
12 #include <mrpt/random.h>
13 #include <mrpt/system.h>
14 #include <gtest/gtest.h>
15 
16 using namespace mrpt;
17 using namespace mrpt::utils;
18 using namespace mrpt::synch;
19 using namespace mrpt::random;
20 using namespace mrpt::system;
21 using namespace std;
22 
23 
24 struct TAux
25 {
26  bool terminated;
27 
28  void (*m_func)(void);
29 
30  void run(int )
31  {
32  if (m_func) (*m_func)(); else throw std::runtime_error("functor is NULL!!");
33  terminated = true;
34  }
35 
36  TAux( void (*func)(void) ) : terminated(false),m_func(func) {}
37 };
38 
39 void launchTestWithTimeout(void (*func)(void), double timeout_secs, const std::string &fail_msg )
40 {
41  TAux obj(func);
42  //mrpt::system::TThreadHandle th =
43  mrpt::system::createThreadFromObjectMethod<TAux>( &obj, &TAux::run, 0 );
44 
46  tim.Tic();
47 
48  while (!obj.terminated && tim.Tac()<timeout_secs) {
50  }
51 
52  if (!obj.terminated) {
53  EXPECT_TRUE(false) << "Thread didn't finished in timeout! While testing: " << fail_msg;
54  }
55 }
56 
57 
58 // TEST 1: Just test if creating a CS doesn't launch an exception or lock.
59 // ----------------------------------------------------------------------------
61 {
63  cs.enter();
64  cs.leave();
65 }
66 TEST(Synch, CriticalSections_Simple )
67 {
68  launchTestWithTimeout(my_CriticalSections_Simple, 5.0, "CriticalSections_Simple");
69 }
70 
71 // TEST 3: Test with several threads competing for a CS:
72 // ----------------------------------------------------------------------------
74 int counter = 0;
75 
76 void thread_example(int id)
77 {
79  try
80  {
81  {
83  counter++;
84  }
85 
86  double delay = randomGenerator.drawUniform(0.1,1.6);
87 
88  //printf("[thread_example %i, ID:%lu] Started, will run for %f seconds\n", id, getCurrentThreadId(), delay);
89 
90  mrpt::system::sleep( delay*1000 );
91 
92  //int remaining;
93  {
95  //remaining =
96  --counter;
97  }
98  }
99  catch(std::exception &e)
100  {
101  cerr << e.what() << endl;
102  }
103  catch(...)
104  {
105  printf("[thread_example] Runtime error!\n");
106  }
107 }
108 
110 {
112 
113  for (int i=1;i<=10;i++)
115 
116  // Wait all threads exit:
117  int cnt;
118  do
119  {
120  sleep(10);
121  csCounter.enter();
122  cnt = counter;
123  csCounter.leave();
124  } while (cnt);
125 }
126 TEST(Synch, CriticalSections_Multi)
127 {
128  launchTestWithTimeout(my_CriticalSections_Multi, 6.0, "CriticalSections_Multi");
129 }
A namespace of pseudo-random numbers genrators of diferent distributions.
double drawUniform(const double Min, const double Max)
Generate a uniformly distributed pseudo-random number using the MT19937 algorithm, scaled to the selected range.
This class provides simple critical sections functionality.
A class acquiring a CCriticalSection at its constructor, and releasing it at destructor.
Classes for serialization, sockets, ini-file manipulation, streams, list of properties-values, timewatch, extensions to STL.
Definition: zip.h:16
This namespace provides a OS-independent interface to many useful functions: filenames manipulation...
Definition: math_frwds.h:29
void my_CriticalSections_Simple()
BASE_IMPEXP CRandomGenerator randomGenerator
A static instance of a CRandomGenerator class, for use in single-thread applications.
void randomize(const uint32_t seed)
Initialize the PRNG from the given random seed.
STL namespace.
void Tic()
Starts the stopwatch.
Definition: CTicTac.cpp:77
GLsizei GLsizei GLuint * obj
Definition: glext.h:3902
TThreadHandle createThread(void(*func)(T), T param)
Creates a new thread from a function (or static method) with one generic parameter.
Definition: threads.h:80
void my_CriticalSections_Multi()
void leave() const MRPT_OVERRIDE
Leave.
#define delay(x)
Definition: win32/timer.h:39
#define MRPT_UNUSED_PARAM(a)
Can be used to avoid "not used parameters" warnings from the compiler.
void BASE_IMPEXP sleep(int time_ms) MRPT_NO_THROWS
An OS-independent method for sending the current thread to "sleep" for a given period of time...
Definition: threads.cpp:57
CCriticalSection csCounter
This class implements a high-performance stopwatch.
Definition: CTicTac.h:24
void enter() const MRPT_OVERRIDE
Enter.
This namespace provides multitask, synchronization utilities.
GLsizei const GLchar ** string
Definition: glext.h:3919
TAux(void(*func)(void))
This is the global namespace for all Mobile Robot Programming Toolkit (MRPT) libraries.
void launchTestWithTimeout(void(*func)(void), double timeout_secs, const std::string &fail_msg)
void thread_example(int id)
typedef void(APIENTRYP PFNGLBLENDCOLORPROC)(GLclampf red
TEST(Synch, CriticalSections_Simple)



Page generated by Doxygen 1.8.14 for MRPT 1.5.7 Git: 5902e14cc Wed Apr 24 15:04:01 2019 +0200 at lun oct 28 01:39:17 CET 2019