aboutsummaryrefslogtreecommitdiff
path: root/src/Window.cpp
diff options
context:
space:
mode:
authordec05eba <dec05eba@protonmail.com>2019-01-12 20:39:23 +0100
committerdec05eba <dec05eba@protonmail.com>2019-01-12 20:39:26 +0100
commitdaa59b89b1f05cf3a2abdee9ef5ac8bffe805b13 (patch)
tree4b027770beec143d9e514819b6ca30d4a9695eab /src/Window.cpp
parentfe4199dace73ee38262528f7ae88cbfba99120dd (diff)
Fix multithreading crashes
Diffstat (limited to 'src/Window.cpp')
-rw-r--r--src/Window.cpp15
1 files changed, 9 insertions, 6 deletions
diff --git a/src/Window.cpp b/src/Window.cpp
index 255986a..8a6652e 100644
--- a/src/Window.cpp
+++ b/src/Window.cpp
@@ -41,7 +41,7 @@ namespace dchat
return result;
}
- Window::Window() :
+ Window::Window() :
chatWindow(this)
{
set_border_width(0);
@@ -230,12 +230,15 @@ namespace dchat
dispatcher.connect([this]()
{
- std::lock_guard<std::mutex> lock(dispatcherMutex);
- for(DispatcherHandler &dispatcherHandler : dispatcherHandlers)
+ std::lock_guard<std::recursive_mutex> lock(dispatcherMutex);
+ // We make a copy because dispatcherHandler below can call main loop again (for example when scrolling) which will call this function,
+ // and we want to prevent the same dispatcher from being called multiple times
+ auto handlersCopies = dispatcherHandlers;
+ dispatcherHandlers.clear();
+ for(DispatcherHandler &dispatcherHandler : handlersCopies)
{
dispatcherHandler();
}
- dispatcherHandlers.clear();
});
Rooms::connect(bootstrapNode->c_str(), 27130, roomCallbackFuncs);
@@ -280,7 +283,7 @@ namespace dchat
void Window::dispatchFunction(DispatcherHandler func)
{
- std::lock_guard<std::mutex> lock(dispatcherMutex);
+ std::lock_guard<std::recursive_mutex> lock(dispatcherMutex);
dispatcherHandlers.push_back(func);
dispatcher.emit();
}
@@ -380,4 +383,4 @@ namespace dchat
queue_draw();
return true;
}
-} \ No newline at end of file
+}