MRPT  1.9.9
test.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-2018, 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/io/CPipe.h>
11 #include <mrpt/poses/CPose2D.h>
12 #include <mrpt/poses/CPose3D.h>
14 #include <thread>
15 #include <iostream>
16 
17 using namespace mrpt;
18 using namespace mrpt::poses;
19 using namespace mrpt::system;
20 using namespace mrpt::io;
21 using namespace mrpt::serialization;
22 using namespace std;
23 
24 void thread_reader(CPipeReadEndPoint& read_pipe)
25 {
26  try
27  {
28  std::cout << "[thread_reader ID:" << std::this_thread::get_id()
29  << "] Started." << std::endl;
30 
31  // Simple read commands:
32  size_t len = 0;
33  char buf[100];
34  read_pipe.Read(&len, sizeof(len));
35  read_pipe.Read(buf, len);
36  buf[len] = 0;
37  cout << "RX: " << buf << endl;
38 
39  // Read MRPT object from a pipe:
40  // *Note*: If the object class is known in advance, one can avoid smart
41  // pointers with ReadObject(&existingObj)
42  auto arch = archiveFrom(read_pipe);
43 #ifndef HAS_BROKEN_CLANG_STD_VISIT
44  auto doprint = [](auto& pose) { cout << "RX pose: " << pose << endl; };
45  auto var =
46  arch.ReadVariant<mrpt::poses::CPose2D, mrpt::poses::CPose3D>();
47  std::visit(doprint, var);
48  var = arch.ReadVariant<mrpt::poses::CPose2D, mrpt::poses::CPose3D>();
49  std::visit(doprint, var);
50 #endif
51 
52  printf("[thread_reader] Finished.\n");
53  }
54  catch (std::exception& e)
55  {
56  cerr << e.what() << endl;
57  }
58 }
59 
60 void thread_writer(CPipeWriteEndPoint& write_pipe)
61 {
62  try
63  {
64  std::cout << "[thread_writer ID:" << std::this_thread::get_id()
65  << "] Started." << std::endl;
66 
67  // Simple write commands:
68  const char* str = "Hello world!";
69  size_t len = strlen(str);
70  write_pipe.Write(&len, sizeof(len));
71  write_pipe.Write(str, len);
72 
73  // Send MRPT objects:
74  mrpt::poses::CPose3D pose1(1, 2, 3, 1.57, 3.14, 0);
75  mrpt::poses::CPose2D pose2(1.0, 2.0, 3.1415);
76  std::variant<mrpt::poses::CPose3D, mrpt::poses::CPose2D> var1(
77  std::move(pose1));
78  std::variant<mrpt::poses::CPose3D, mrpt::poses::CPose2D> var2(
79  std::move(pose2));
80  auto arch = archiveFrom(write_pipe);
81 #ifndef HAS_BROKEN_CLANG_STD_VISIT
82  arch.WriteVariant(var1);
83  arch.WriteVariant(var2);
84 #endif
85  printf("[thread_writer] Finished.\n");
86  }
87  catch (std::exception& e)
88  {
89  cerr << e.what() << endl;
90  }
91 }
92 
93 // ------------------------------------------------------
94 // ThreadsTest
95 // ------------------------------------------------------
96 void ThreadsTest()
97 {
98  // Create a pipe:
99  std::unique_ptr<CPipeReadEndPoint> read_pipe;
100  std::unique_ptr<CPipeWriteEndPoint> write_pipe;
101 
102  CPipe::createPipe(read_pipe, write_pipe);
103 
104  // And send the two end-points to two threads:
105  std::thread hT1(thread_reader, std::ref(*read_pipe));
106  std::thread hT2(thread_writer, std::ref(*write_pipe));
107 
108  using namespace std::chrono_literals;
109  std::this_thread::sleep_for(10ms);
110  // Wait for the threads to end.
111  hT2.join();
112  // We need to close this to ensure the pipe gets flushed
113  // Remember Unix uses buffered io
114  // write_pipe.reset();
115  hT1.join();
116 }
117 
118 // ------------------------------------------------------
119 // MAIN
120 // ------------------------------------------------------
121 int main()
122 {
123  try
124  {
125  ThreadsTest();
126 
127  return 0;
128  }
129  catch (std::exception& e)
130  {
131  std::cout << "MRPT exception caught: " << e.what() << std::endl;
132  return -1;
133  }
134  catch (...)
135  {
136  printf("Untyped exception!!");
137  return -1;
138  }
139 }
virtual size_t Read(void *Buffer, size_t Count) override
Introduces a pure virtual method responsible for reading from the stream.
Definition: CPipe.cpp:120
void thread_reader(CPipeReadEndPoint &read_pipe)
GLenum GLint ref
Definition: glext.h:4050
STL namespace.
GLenum GLsizei len
Definition: glext.h:4712
CArchiveStreamBase< STREAM > archiveFrom(STREAM &s)
Helper function to create a templatized wrapper CArchive object for a: MRPT&#39;s CStream, std::istream, std::ostream, std::stringstream
Definition: CArchive.h:555
virtual size_t Write(const void *Buffer, size_t Count) override
Introduces a pure virtual method responsible for writing to the stream.
Definition: CPipe.cpp:219
Classes for 2D/3D geometry representation, both of single values and probability density distribution...
The write end-point in a pipe created with mrpt::synch::CPipe.
Definition: CPipe.h:149
This is the global namespace for all Mobile Robot Programming Toolkit (MRPT) libraries.
A class used to store a 2D pose, including the 2D coordinate point and a heading (phi) angle...
Definition: CPose2D.h:38
A class used to store a 3D pose (a 3D translation + a rotation in 3D).
Definition: CPose3D.h:86
The read end-point in a pipe created with mrpt::synch::CPipe.
Definition: CPipe.h:126
void ThreadsTest()
void thread_writer(CPipeWriteEndPoint &write_pipe)



Page generated by Doxygen 1.8.14 for MRPT 1.9.9 Git: 7d5e6d718 Fri Aug 24 01:51:28 2018 +0200 at lun nov 2 08:35:50 CET 2020