add mutex for thread safety
diff --git a/demo/chatdialog.cpp b/demo/chatdialog.cpp
index c935c66..0f6e806 100644
--- a/demo/chatdialog.cpp
+++ b/demo/chatdialog.cpp
@@ -87,6 +87,7 @@
void
ChatDialog::appendMessage(const SyncDemo::ChatMessage msg)
{
+ boost::mutex::scoped_lock lock(m_msgMutex);
if (msg.type() != SyncDemo::ChatMessage::CHAT) {
return;
@@ -133,7 +134,10 @@
}
// reflect the changes on digest tree
- m_scene->processUpdate(v, m_sock->getRootDigest().c_str());
+ {
+ boost::mutex::scoped_lock lock(m_sceneMutex);
+ m_scene->processUpdate(v, m_sock->getRootDigest().c_str());
+ }
int n = v.size();
int totalMissingPackets = 0;
@@ -200,7 +204,10 @@
#ifdef __DEBUG
std::cout <<"<<< updating scene for" << prefix << ": " << msg.from() << std::endl;
#endif
- m_scene->msgReceived(prefix.c_str(), msg.from().c_str());
+ {
+ boost::mutex::scoped_lock lock(m_sceneMutex);
+ m_scene->msgReceived(prefix.c_str(), msg.from().c_str());
+ }
fitView();
}
@@ -292,8 +299,11 @@
Sync::MissingDataInfo mdi = {m_user.getPrefix().toStdString(), Sync::SeqNo(0), Sync::SeqNo(nextSequence - 1)};
std::vector<Sync::MissingDataInfo> v;
v.push_back(mdi);
- m_scene->processUpdate(v, m_sock->getRootDigest().c_str());
- m_scene->msgReceived(m_user.getPrefix(), m_user.getNick());
+ {
+ boost::mutex::scoped_lock lock(m_sceneMutex);
+ m_scene->processUpdate(v, m_sock->getRootDigest().c_str());
+ m_scene->msgReceived(m_user.getPrefix(), m_user.getNick());
+ }
fitView();
}
@@ -332,8 +342,11 @@
m_user.setChatroom(chatroom);
needWrite = true;
- m_scene->clearAll();
- m_scene->plot("Empty");
+ {
+ boost::mutex::scoped_lock lock(m_sceneMutex);
+ m_scene->clearAll();
+ m_scene->plot("Empty");
+ }
// TODO: perhaps need to do a lot. e.g. use a new SyncAppSokcet
if (m_sock != NULL)
{
@@ -377,6 +390,7 @@
void
ChatDialog::fitView()
{
+ boost::mutex::scoped_lock lock(m_sceneMutex);
QRectF rect = m_scene->itemsBoundingRect();
m_scene->setSceneRect(rect);
treeViewer->fitInView(m_scene->itemsBoundingRect(), Qt::KeepAspectRatio);
diff --git a/demo/chatdialog.h b/demo/chatdialog.h
index 312ebb0..e5e0c34 100644
--- a/demo/chatdialog.h
+++ b/demo/chatdialog.h
@@ -1,6 +1,7 @@
#ifndef CHATDIALOG_H
#define CHATDIALOG_H
#include <boost/function.hpp>
+#include <boost/thread/mutex.hpp>
#include <vector>
#include "digesttreescene.h"
#include "ui_chatdialog.h"
@@ -53,5 +54,7 @@
Sync::SyncAppSocket *m_sock;
uint32_t m_session;
DigestTreeScene *m_scene;
+ boost::mutex m_msgMutex;
+ boost::mutex m_sceneMutex;
};
#endif