Main MRPT website > C++ reference for MRPT 1.5.9
CSemaphore_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/CSemaphore.h>
11 #include <mrpt/random.h>
12 #include <mrpt/system/threads.h>
13 #include <gtest/gtest.h>
14 
15 using namespace mrpt;
16 using namespace mrpt::utils;
17 using namespace mrpt::synch;
18 using namespace mrpt::random;
19 using namespace mrpt::system;
20 using namespace std;
21 
22 // External in other .cpp
23 void launchTestWithTimeout(void (*func)(void), double timeout_secs, const std::string &fail_msg );
24 
25 //#define DEBUG_OUT
26 
27 // TEST 1: Test named semaphores
28 // ----------------------------------------------------------------------------
29 void sem_thread_example(int id)
30 {
31  try
32  {
33 #ifdef DEBUG_OUT
34  cout << mrpt::format("[thread_example2 %i, ID:%lu] Started, trying to get into semaphore...\n", id, getCurrentThreadId()); cout.flush();
35 #else
37 #endif // DEBUG_OUT
38 
39  CSemaphore sem(1,1);
40 #ifdef DEBUG_OUT
41  cout << mrpt::format("[thread_example2 %i, ID:%lu] Object created...\n", id, getCurrentThreadId()); cout.flush();
42 #endif // DEBUG_OUT
43 
44  sem.waitForSignal();
45 
46  double delay = randomGenerator.drawUniform(0.1,0.5);
47 #ifdef DEBUG_OUT
48  cout << mrpt::format("[thread_example2 %i, ID:%lu] I'm in. Delaying %f seconds...\n", id, getCurrentThreadId(), delay); cout.flush();
49 #endif // DEBUG_OUT
50  mrpt::system::sleep( delay*1000 );
51 
52 #ifdef DEBUG_OUT
53  cout << mrpt::format("[thread_example2 %i] Releasing..\n", id);cout.flush();
54 #endif // DEBUG_OUT
55  sem.release();
56  }
57  catch(std::exception &e)
58  {
59  cerr << e.what() << endl;
60  }
61  catch(...)
62  {
63  printf("[thread_example2] Runtime error!\n");
64  }
65 }
66 
67 template <int num_threads,int initial_sem_count>
69 {
70  std::vector<TThreadHandle> threads;
71 
72  // Create a named semaphore:
73  CSemaphore sem(initial_sem_count /*init val*/,5*num_threads /*max val*/);
74 
75  mrpt::system::sleep(1000);
76 
77  for (int i=1;i<=num_threads;i++)
78  threads.push_back( createThread( sem_thread_example, i ) );
79 
80  // Wait all threads exit:
81  for (size_t i=0;i<threads.size();i++)
82  joinThread( threads[i] );
83 }
84 
86 {
87  launchTestWithTimeout(my_CSemaphore<6,1>, 5.0, "CSemaphore: #threads=6, init count=1");
88  launchTestWithTimeout(my_CSemaphore<10,5>, 5.0, "CSemaphore: #threads=10, init count=5");
89 }
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.
Classes for serialization, sockets, ini-file manipulation, streams, list of properties-values, timewatch, extensions to STL.
Definition: zip.h:16
unsigned long BASE_IMPEXP getCurrentThreadId() MRPT_NO_THROWS
Returns the ID of the current thread.
Definition: threads.cpp:83
void launchTestWithTimeout(void(*func)(void), double timeout_secs, const std::string &fail_msg)
This namespace provides a OS-independent interface to many useful functions: filenames manipulation...
Definition: math_frwds.h:29
BASE_IMPEXP CRandomGenerator randomGenerator
A static instance of a CRandomGenerator class, for use in single-thread applications.
bool waitForSignal(unsigned int timeout_ms=0)
Blocks until the count of the semaphore to be non-zero.
Definition: CSemaphore.cpp:24
STL namespace.
void my_CSemaphore()
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
#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
void sem_thread_example(int id)
void release(unsigned int increaseCount=1)
Increments the count of the semaphore by a given amount.
Definition: CSemaphore.cpp:41
This namespace provides multitask, synchronization utilities.
std::string BASE_IMPEXP format(const char *fmt,...) MRPT_printf_format_check(1
A std::string version of C sprintf.
GLsizei const GLchar ** string
Definition: glext.h:3919
void BASE_IMPEXP joinThread(TThreadHandle &threadHandle)
Waits until the given thread ends.
Definition: threads.cpp:74
This is the global namespace for all Mobile Robot Programming Toolkit (MRPT) libraries.
TEST(Synch, CSemaphore)
A semaphore for inter-thread synchronization.
Definition: CSemaphore.h:31



Page generated by Doxygen 1.8.14 for MRPT 1.5.9 Git: 690a4699f Wed Apr 15 19:29:53 2020 +0200 at miƩ abr 15 19:30:12 CEST 2020