From c61ab7c4232eb80b7cc3c2f37ba2715e16b4ee73 Mon Sep 17 00:00:00 2001 From: deva Date: Sun, 27 Mar 2005 10:29:50 +0000 Subject: Made the Error object thread safe. --- src/error.cc | 16 +++++++++++++++- src/error.h | 8 ++++++-- src/mainwindow.cc | 1 - 3 files changed, 21 insertions(+), 4 deletions(-) (limited to 'src') diff --git a/src/error.cc b/src/error.cc index b166042..4e3f045 100644 --- a/src/error.cc +++ b/src/error.cc @@ -30,12 +30,21 @@ Error::Error() { // Initialize lastError = NULL; + + pthread_mutex_init (&mutex, NULL); } Error::~Error() { } +/* + pthread_mutex_lock(&mutex); + // ... do something + pthread_mutex_unlock(&mutex); + + */ + bool Error::hasError() { return lastError != NULL; @@ -52,17 +61,20 @@ string Error::popAllErrorStrings() string Error::popErrorString() { + pthread_mutex_lock(&mutex); if(lastError == NULL) return string(""); _err_entry *err = lastError; string le = err->errstr; lastError = err->prev; delete err; + pthread_mutex_unlock(&mutex); return le; } void Error::pushError(char* errstr) { + pthread_mutex_lock(&mutex); printf("New Error: [%s]\n", errstr); _err_entry *err = new _err_entry; @@ -70,19 +82,21 @@ void Error::pushError(char* errstr) err->errstr = errstr; err->prev = lastError; lastError = err; + pthread_mutex_unlock(&mutex); } void Error::pushError(string &errstr) { + pthread_mutex_lock(&mutex); _err_entry *err = new _err_entry; err->errstr = errstr; err->prev = lastError; lastError = err; + pthread_mutex_unlock(&mutex); } void Error::removeAllErrors() { while(lastError) popErrorString(); - } diff --git a/src/error.h b/src/error.h index 8b26471..a7e6b29 100644 --- a/src/error.h +++ b/src/error.h @@ -30,6 +30,9 @@ #include using namespace std; +#include +#include + /** * This struct contains one error, and a pointer to the previous one. */ @@ -54,8 +57,9 @@ public: void removeAllErrors(); private: - // Used to save the state of the network and camera connections. - bool error; + pthread_mutex_t mutex; + + // A pointer to the last error. _err_entry *lastError; }; diff --git a/src/mainwindow.cc b/src/mainwindow.cc index e779c19..8164981 100644 --- a/src/mainwindow.cc +++ b/src/mainwindow.cc @@ -95,7 +95,6 @@ MainWindow::MainWindow( QWidget* parent, const char* name ) cfg.readInt("server_port")); cam_error = camera->errorObject(); - cam_error->pushError("fisk"); while(cam_error->hasError()) { MessageBox(this, "", cam_error->popErrorString().c_str(), TYPE_OK, ICON_ERROR).exec(); } -- cgit v1.2.3