Index: trunk/Cosy/base/msgqueue.cc
===================================================================
--- trunk/Cosy/base/msgqueue.cc	(revision 16767)
+++ trunk/Cosy/base/msgqueue.cc	(revision 16779)
@@ -1,3 +1,5 @@
 #include "msgqueue.h"
+
+#include <iostream>
 
 using namespace std;
@@ -7,7 +9,6 @@
 // This creates the Message queue thread,
 //
-MsgQueue::MsgQueue() : MThread("MsqQueue"), fNextMsg(0), fNextPtr(0)
+MsgQueue::MsgQueue() : fState(kRun), fThread(std::bind(&MsgQueue::Thread, this))
 {
-    RunThread();
 }
 
@@ -19,14 +20,15 @@
 {
     CancelThread();
+}
 
-    fMuxMsg.Lock();
+void MsgQueue::CancelThread()
+{
+    const std::lock_guard<std::mutex> lock(fMutex);
+    if (fState!=kIdle)
+        return;
 
-    if (fNextPtr)
-    {
-        delete [] fNextPtr;
-        fNextPtr = 0;
-    }
-
-    fMuxMsg.UnLock();
+    fState = kAbort;
+    fCond.notify_one();
+    fThread.join();
 }
 
@@ -43,5 +45,5 @@
 // the old action can be finished correctly by the user.
 //
-Int_t MsgQueue::Proc(int msg, void *mp)
+int MsgQueue::Proc(int msg, void *mp)
 {
     return 0;
@@ -50,5 +52,5 @@
 int MsgQueue::Break() const
 {
-    return fNextMsg>0 || IsThreadCanceled();
+    return fSize>0 || fState!=kRun;
 }
 
@@ -60,34 +62,33 @@
 // in this thread. This makes sure, that the calling program is not stalled.
 //
-Int_t MsgQueue::Thread()
+void MsgQueue::Thread()
 {
+    std::unique_lock<std::mutex> lock(fMutex);
+
     while (1)
     {
-        Int_t msg = 0;
-        char *ptr = 0;
+        while (fList.empty() && fState==kRun)
+            fCond.wait(lock);
 
-        fMuxMsg.Lock();
+        if (fState==kAbort)
+            break;
 
-        if (fNextMsg)
-        {
-            msg = fNextMsg;
-            ptr = fNextPtr;
+        if (fState==kStop && fList.empty())
+            break;
 
-            fNextMsg = 0;
-            fNextPtr = 0;
-        }
+        const auto &val = fList.front();
+        fSize--;
 
-        fMuxMsg.UnLock();
+        lock.unlock();
+        Proc(val.first, const_cast<char*>(val.second.data()));
+        lock.lock();
 
-        if (ptr)
-        {
-            Proc(msg, ptr);
-            delete [] ptr;
-        }
+        fList.pop_front();
+    }
 
-        usleep(1);
-        TThread::CancelPoint();
-    }
-    return 0;
+    fList.clear();
+    fSize = 0;
+
+    fState = kIdle;
 }
 
@@ -102,16 +103,14 @@
 void *MsgQueue::PostMsg(int msg, void *mp, int size)
 {
-    fMuxMsg.Lock();
+    const std::lock_guard<std::mutex> lock(fMutex);
+    if (fState==kIdle)
+        return false;
 
-    fNextMsg = msg;
+    fSize++;
+    fList.emplace_back(msg, vector<char>(reinterpret_cast<char*>(mp),reinterpret_cast<char*>(mp)+size));
 
-    if (fNextPtr)
-        delete [] fNextPtr;
-    fNextPtr = new char[size];
-
-    memcpy(fNextPtr, mp, size);
-
-    fMuxMsg.UnLock();
+    fCond.notify_one();
 
     return 0;
+
 }
Index: trunk/Cosy/base/msgqueue.h
===================================================================
--- trunk/Cosy/base/msgqueue.h	(revision 16767)
+++ trunk/Cosy/base/msgqueue.h	(revision 16779)
@@ -2,10 +2,10 @@
 #define COSY_MsgQueue
 
-#ifndef ROOT_TMutex
-#include <TMutex.h>
-#endif
-
-#ifndef MARS_MThread
-#include "MThread.h"
+#ifndef __CINT__
+#include <list>
+#include <mutex>
+#include <thread>
+#include <vector>
+#include <condition_variable>
 #endif
 
@@ -13,13 +13,30 @@
 #define WM_QUIT 0xffff
 
-class MsgQueue : public MThread
+class MsgQueue
 {
 private:
-    int    fNextMsg;
-    char  *fNextPtr;
+    size_t fSize;                 // Only necessary for before C++11
 
-    TMutex fMuxMsg;
+#ifndef __CINT__
+    std::list<std::pair<int, std::vector<char>>> fList;
 
-    Int_t Thread();
+    std::mutex fMutex;        // Mutex needed for the conditional
+    std::condition_variable fCond; // Conditional
+#endif
+
+    enum state_t
+    {
+        kIdle,
+        kRun,
+        kStop,
+        kAbort,
+    };
+
+    state_t fState;               // Stop signal for the thread
+#ifndef __CINT__
+    std::thread fThread;      // Handle to the thread
+#endif
+
+    void Thread();
 
 public:
@@ -29,6 +46,8 @@
     int Break() const;
 
-    virtual Int_t Proc(int msg, void *mp1);
-    Int_t Proc(int msg) { return Proc(msg, 0); }
+    void CancelThread();
+
+    virtual int Proc(int msg, void *mp1);
+    int Proc(int msg) { return Proc(msg, 0); }
 
     void *PostMsg(int msg, void *mp1, int size);
