HEAD without the locks in Graph.cpp

It's a typical race condition, see output with attached patch:



184 74127768 size 1 , thread:
186 74127768 size 1 , thread:
36 19714232 size 0 , thread:



This means
line 184: thread 74127768 checks if Q is not empty > enter the while loop
line 186: Q is still not empty, the thread will call front in the next line
line  36: But in he meantime a other thread has cleared Q so it craches
          when thread 74127768 calls front()



Peter
Index: Graph.cpp
===================================================================
--- Graph.cpp	(revision 37227)
+++ Graph.cpp	(working copy)
@@ -19,6 +19,9 @@
 
 #include <algorithm>
 
+#include <QDebug>
+#include <QThread>
+
 using namespace std;
 
 namespace lyx {
@@ -30,6 +33,7 @@
 		return false;
 
 	Q_ = queue<int>();
+  qDebug() << __LINE__ << (int)QThread::currentThread() << "size" << Q_.size() << ", thread: ";
 
 	if (clear_visited) {
 		vector<Vertex>::iterator it = vertices_.begin();
@@ -57,7 +61,7 @@
 vector<int> const
 	Graph::getReachableTo(int target, bool clear_visited)
 {
-	Mutex::Locker lock(&mutex_);
+	//Mutex::Locker lock(&mutex_);
 
 	vector<int> result;
 	if (!bfs_init(target, clear_visited))
@@ -94,7 +98,7 @@
 	Graph::getReachable(int from, bool only_viewable,
 		bool clear_visited)
 {
-	Mutex::Locker lock(&mutex_);
+	//Mutex::Locker lock(&mutex_);
 
 	vector<int> result;
 	if (!bfs_init(from, clear_visited))
@@ -132,7 +136,7 @@
 
 bool Graph::isReachable(int from, int to)
 {
-	Mutex::Locker lock(&mutex_);
+	//Mutex::Locker lock(&mutex_);
 
 	if (from == to)
 		return true;
@@ -165,18 +169,25 @@
 
 Graph::EdgePath const Graph::getPath(int from, int to)
 {
-	Mutex::Locker lock(&mutex_);
+	//Mutex::Locker lock(&mutex_);
 
+  //qDebug() << __LINE__ << (int)QThread::currentThread() << "Graph:" << (int) this;
+
 	if (from == to)
 		return EdgePath();
 
 	if (to < 0 || !bfs_init(from))
 		return EdgePath();
 
+
 	clearPaths();
+  qDebug() << __LINE__ << (int)QThread::currentThread() << "size" << Q_.size() << ", thread: ";
 	while (!Q_.empty()) {
+    qDebug() << __LINE__ << (int)QThread::currentThread() << "size" << Q_.size() << ", thread: ";
 		int const current = Q_.front();
+    qDebug() << __LINE__ << (int)QThread::currentThread() << "size" << Q_.size() << ", thread: ";
 		Q_.pop();
+    qDebug() << __LINE__ << (int)QThread::currentThread() << "size" << Q_.size() << ", thread: ";
 
 		vector<Arrow *>::const_iterator cit =
 			vertices_[current].out_arrows.begin();
@@ -206,7 +217,7 @@
 
 void Graph::init(int size)
 {
-	Mutex::Locker lock(&mutex_);
+	//Mutex::Locker lock(&mutex_);
 
 	vertices_ = vector<Vertex>(size);
 	arrows_.clear();
@@ -216,7 +227,7 @@
 
 void Graph::addEdge(int from, int to)
 {
-	Mutex::Locker lock(&mutex_);
+	//Mutex::Locker lock(&mutex_);
 
 	arrows_.push_back(Arrow(from, to, numedges_));
 	numedges_++;

Reply via email to