Point Cloud Library (PCL)  1.9.1-dev
synchronized_queue.hpp
1 /*
2  * Software License Agreement (BSD License)
3  *
4  * Point Cloud Library (PCL) - www.pointclouds.org
5  * Copyright (c) 2012
6  *
7  * All rights reserved.
8  *
9  * Redistribution and use in source and binary forms, with or without
10  * modification, are permitted provided that the following conditions
11  * are met:
12  *
13  * * Redistributions of source code must retain the above copyright
14  * notice, this list of conditions and the following disclaimer.
15  * * Redistributions in binary form must reproduce the above
16  * copyright notice, this list of conditions and the following
17  * disclaimer in the documentation and/or other materials provided
18  * with the distribution.
19  * * Neither the name of the copyright holder(s) nor the names of its
20  * contributors may be used to endorse or promote products derived
21  * from this software without specific prior written permission.
22  *
23  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
24  * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
25  * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
26  * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
27  * COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
28  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
29  * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
30  * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
31  * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
32  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
33  * ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
34  * POSSIBILITY OF SUCH DAMAGE.
35  *
36  */
37 
38 /* SynchronizedQueue Template, taken from
39  * http://stackoverflow.com/questions/10139251/shared-queue-c
40  */
41 #pragma once
42 
43 #include <condition_variable>
44 #include <mutex>
45 #include <queue>
46 
47 namespace pcl
48 {
49 
50  template<typename T>
52  {
53  public:
54 
56  queue_(), request_to_end_(false), enqueue_data_(true) { }
57 
58  void
59  enqueue (const T& data)
60  {
61  std::unique_lock<std::mutex> lock (mutex_);
62 
63  if (enqueue_data_)
64  {
65  queue_.push (data);
66  cond_.notify_one ();
67  }
68  }
69 
70  bool
71  dequeue (T& result)
72  {
73  std::unique_lock<std::mutex> lock (mutex_);
74 
75  while (queue_.empty () && (!request_to_end_))
76  {
77  cond_.wait (lock);
78  }
79 
80  if (request_to_end_)
81  {
82  doEndActions ();
83  return false;
84  }
85 
86  result = queue_.front ();
87  queue_.pop ();
88 
89  return true;
90  }
91 
92  void
94  {
95  std::unique_lock<std::mutex> lock (mutex_);
96  request_to_end_ = true;
97  cond_.notify_one ();
98  }
99 
100  unsigned int
101  size ()
102  {
103  std::unique_lock<std::mutex> lock (mutex_);
104  return static_cast<unsigned int> (queue_.size ());
105  }
106 
107  bool
108  isEmpty () const
109  {
110  std::unique_lock<std::mutex> lock (mutex_);
111  return (queue_.empty ());
112  }
113 
114  private:
115  void
116  doEndActions ()
117  {
118  enqueue_data_ = false;
119 
120  while (!queue_.empty ())
121  {
122  queue_.pop ();
123  }
124  }
125 
126  std::queue<T> queue_; // Use STL queue to store data
127  mutable std::mutex mutex_; // The mutex to synchronise on
128  std::condition_variable cond_; // The condition to wait for
129 
130  bool request_to_end_;
131  bool enqueue_data_;
132  };
133 }
This file defines compatibility wrappers for low level I/O functions.
Definition: convolution.h:45
void enqueue(const T &data)