Index: /trunk/FACT++/src/queue.h
===================================================================
--- /trunk/FACT++/src/queue.h	(revision 16093)
+++ /trunk/FACT++/src/queue.h	(revision 16094)
@@ -6,5 +6,5 @@
 
 template<class T>
-class Queue : std::deque<T>
+class Queue : std::list<T>
 {
     std::mutex fMutex;        // Mutex needed for the conditional
@@ -13,8 +13,11 @@
     enum state_t
     {
+        kIdle,
         kRun,
-        kWait,
-        kStop
+        kStop,
+        kAbort,
     };
+
+    size_t fSize;                 // Only necessary for before C++11
 
     state_t fState;               // Stop signal for the thread
@@ -28,17 +31,17 @@
     {
         std::unique_lock<std::mutex> lock(fMutex);
+
         while (1)
         {
-            while (std::deque<T>::empty() && fState==kRun)
+            while (std::list<T>::empty() && fState==kRun)
                 fCond.wait(lock);
 
-            if (fState==kStop)
+            if (fState==kAbort)
                 break;
 
-            if (fState==kWait && std::deque<T>::empty())
+            if (fState==kStop && std::list<T>::empty())
                 break;
 
-            const T val = std::deque<T>::front();
-            std::deque<T>::pop_front();
+            const T &val = std::list<T>::front();
 
             // Theoretically, we can loose a signal here, but this is
@@ -50,52 +53,95 @@
 
             lock.lock();
+
+            std::list<T>::pop_front();
+            fSize--;
         }
+
+        std::list<T>::clear();
+        fSize = 0;
+
+        fState = kIdle;
     }
 
 public:
-    Queue(const callback &f) : fState(kRun), fCallback(f)
+    Queue(const callback &f) : fSize(0), fState(kIdle), fCallback(f)
     {
-        fThread = std::thread(std::bind(&Queue::Thread, this));
+        start();
     }
     ~Queue()
     {
-        stop();
-        join();
+        wait(true);
     }
 
-    void post(const T &val)
+    bool start()
     {
         const std::lock_guard<std::mutex> lock(fMutex);
+        if (fState!=kIdle)
+            return false;
 
-        std::deque<T>::push_back(val);
-        fCond.notify_one();
+        fState = kRun;
+        fThread = std::thread(std::bind(&Queue::Thread, this));
+        return true;
     }
 
-    void wait()
+    bool stop()
     {
-        fMutex.lock();
-        fState = kWait;
+        const std::lock_guard<std::mutex> lock(fMutex);
+        if (fState==kIdle)
+            return false;
+
+        fState = kStop;
         fCond.notify_one();
-        fMutex.unlock();
+
+        return true;
     }
 
-    void stop()
+    bool abort()
     {
-        fMutex.lock();
-        fState = kStop;
+        const std::lock_guard<std::mutex> lock(fMutex);
+        if (fState==kIdle)
+            return false;
+
+        fState = kAbort;
         fCond.notify_one();
-        fMutex.unlock();
+
+        return true;
     }
 
-    void join()
+    bool wait(bool abrt=false)
     {
-        // This can happen is the thread is not running anymore
-        try
         {
-            fThread.join();
+            const std::lock_guard<std::mutex> lock(fMutex);
+            if (fState==kIdle)
+                return false;
+
+            if (fState==kRun)
+            {
+                fState = abrt ? kAbort : kStop;
+                fCond.notify_one();
+            }
         }
-        catch (const std::system_error &)
-        {
-        }
+
+        fThread.join();
+        return true;
+    }
+
+    bool post(const T &val)
+    {
+        const std::lock_guard<std::mutex> lock(fMutex);
+        if (fState==kIdle)
+            return false;
+
+        std::list<T>::push_back(val);
+        fSize++;
+
+        fCond.notify_one();
+
+        return true;
+    }
+
+    size_t size() const
+    {
+        return fSize;
     }
 };
