SyncQueue.h

Go to the documentation of this file.
00001 /* ========================================================================
00002  * Copyright (c) 2006,
00003  * Institute for Computer Graphics and Vision
00004  * Graz University of Technology
00005  * All rights reserved.
00006  *
00007  * Redistribution and use in source and binary forms, with or without
00008  * modification, are permitted provided that the following conditions are
00009  * met:
00010  *
00011  * Redistributions of source code must retain the above copyright notice,
00012  * this list of conditions and the following disclaimer.
00013  *
00014  * Redistributions in binary form must reproduce the above copyright
00015  * notice, this list of conditions and the following disclaimer in the
00016  * documentation and/or other materials provided with the distribution.
00017  *
00018  * Neither the name of the Graz University of Technology nor the names of
00019  * its contributors may be used to endorse or promote products derived from
00020  * this software without specific prior written permission.
00021  *
00022  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS
00023  * IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
00024  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A
00025  * PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER
00026  * OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
00027  * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
00028  * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
00029  * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
00030  * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
00031  * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
00032  * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
00033  * ========================================================================
00034  * PROJECT: OpenTracker
00035  * ======================================================================== */
00042 /* ======================================================================= */
00043 #ifndef OT_TOOL_SYNCQUEUE_H
00044 #define OT_TOOL_SYNCQUEUE_H
00045 #include <queue>
00046 namespace ot{
00047 
00048   template<class T>
00049   class SyncQueue{
00050   private:
00051     std::queue<T> msgQueue;
00052     ACE_Thread_Mutex _mutex;  
00053   
00054     ACE_Thread_Mutex _sigMutex;
00055 
00056     ACE_Condition<ACE_Thread_Mutex> haveDataSignal;
00057 
00058   public:
00059     //delete all the messages in the queue
00060     void cleanupAll(){
00061       _mutex.acquire();
00062       while(!msgQueue.empty()){
00063         delete msgQueue.front();
00064         msgQueue.pop();
00065       }
00066       _mutex.release();
00067    
00068     };
00069 
00070   public:
00071     SyncQueue():_mutex("SyncQueueMutex"),_sigMutex("SyncQueueHaveData"),
00072                 haveDataSignal(_sigMutex, USYNC_THREAD, "SyncQueueDataSignal", NULL){
00073     };
00074 
00075     ~SyncQueue(){
00076       removeAll();
00077     };
00078   
00079     void push(T msg){
00080       _mutex.acquire();
00081       msgQueue.push(msg);
00082 
00083       haveDataSignal.broadcast();
00084       _mutex.release();
00085     };
00086 
00087     T front(){
00088       T result=0;
00089       _mutex.acquire();
00090       if (!msgQueue.empty()){
00091         result = msgQueue.front();
00092         msgQueue.pop();
00093       }
00094       _mutex.release();
00095       return result;
00096     };
00097 
00098     void removeAll(){
00099       _mutex.acquire();
00100       while(!msgQueue.empty()){
00101         //      delete msgQueue.front();
00102         msgQueue.pop();
00103       }
00104       _mutex.release();
00105     };
00106     bool isEmpty(){
00107       bool result = true;
00108       _mutex.acquire();
00109       result = msgQueue.empty();
00110       _mutex.release();
00111       return result;
00112     };
00113 
00114     void waitForData(){
00115       _sigMutex.acquire();
00116       haveDataSignal.wait();
00117       _sigMutex.release();
00118     }
00119   
00120   };
00121 
00122 
00123 
00124 };
00125 
00126 #endif //OT_TOOL_SYNCQUEUE_H

Generated on Wed Feb 28 15:18:51 2007 for NaviTrack by  doxygen 1.4.6