synchronized_queue.hpp
Go to the documentation of this file.
00001 /*
00002  * Software License Agreement (BSD License)
00003  *
00004  *  Point Cloud Library (PCL) - www.pointclouds.org
00005  *  Copyright (c) 2012
00006  *
00007  *  All rights reserved.
00008  *
00009  *  Redistribution and use in source and binary forms, with or without
00010  *  modification, are permitted provided that the following conditions
00011  *  are met:
00012  *
00013  *   * Redistributions of source code must retain the above copyright
00014  *     notice, this list of conditions and the following disclaimer.
00015  *   * Redistributions in binary form must reproduce the above
00016  *     copyright notice, this list of conditions and the following
00017  *     disclaimer in the documentation and/or other materials provided
00018  *     with the distribution.
00019  *   * Neither the name of the copyright holder(s) nor the names of its
00020  *     contributors may be used to endorse or promote products derived
00021  *     from this software without specific prior written permission.
00022  *
00023  *  THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
00024  *  "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
00025  *  LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
00026  *  FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
00027  *  COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
00028  *  INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
00029  *  BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
00030  *  LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
00031  *  CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
00032  *  LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
00033  *  ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
00034  *  POSSIBILITY OF SUCH DAMAGE.
00035  *
00036  */
00037 
00038 /* SynchronizedQueue Template, taken from
00039  * http://stackoverflow.com/questions/10139251/shared-queue-c
00040  */
00041 
00042 #ifndef SYNCHRONIZED_QUEUE_H_
00043 #define SYNCHRONIZED_QUEUE_H_
00044 
00045 #include <queue>
00046 
00047 namespace pcl
00048 {
00049 
00050   template<typename T>
00051   class SynchronizedQueue
00052   {
00053     public:
00054 
00055       SynchronizedQueue () :
00056         queue_(), mutex_(), cond_(), request_to_end_(false), enqueue_data_(true) { }
00057 
00058       void
00059       enqueue (const T& data)
00060       {
00061         boost::unique_lock<boost::mutex> lock (mutex_);
00062 
00063         if (enqueue_data_)
00064         {
00065           queue_.push (data);
00066           cond_.notify_one ();
00067         }
00068       }
00069 
00070       bool
00071       dequeue (T& result)
00072       {
00073         boost::unique_lock<boost::mutex> lock (mutex_);
00074 
00075         while (queue_.empty () && (!request_to_end_))
00076         {
00077           cond_.wait (lock);
00078         }
00079 
00080         if (request_to_end_)
00081         {
00082           doEndActions ();
00083           return false;
00084         }
00085 
00086         result = queue_.front ();
00087         queue_.pop ();
00088 
00089         return true;
00090       }
00091 
00092       void
00093       stopQueue ()
00094       {
00095         boost::unique_lock<boost::mutex> lock (mutex_);
00096         request_to_end_ = true;
00097         cond_.notify_one ();
00098       }
00099 
00100       unsigned int
00101       size ()
00102       {
00103         boost::unique_lock<boost::mutex> lock (mutex_);
00104         return static_cast<unsigned int> (queue_.size ());
00105       }
00106 
00107       bool
00108       isEmpty () const
00109       {
00110         boost::unique_lock<boost::mutex> lock (mutex_);
00111         return (queue_.empty ());
00112       }
00113 
00114     private:
00115       void
00116       doEndActions ()
00117       {
00118         enqueue_data_ = false;
00119 
00120         while (!queue_.empty ())
00121         {
00122           queue_.pop ();
00123         }
00124       }
00125 
00126       std::queue<T> queue_;              // Use STL queue to store data
00127       mutable boost::mutex mutex_;       // The mutex to synchronise on
00128       boost::condition_variable cond_;   // The condition to wait for
00129 
00130       bool request_to_end_;
00131       bool enqueue_data_;
00132   };
00133 }
00134 #endif /* SYNCHRONIZED_QUEUE_H_ */


pcl
Author(s): Open Perception
autogenerated on Wed Aug 26 2015 15:34:15