]> git.sesse.net Git - pkanalytics/commitdiff
Fix a threading issue, where we would call into the Qt thread from the video thread.
authorSteinar H. Gunderson <sgunderson@bigfoot.com>
Thu, 20 Jul 2023 13:42:40 +0000 (15:42 +0200)
committerSteinar H. Gunderson <sgunderson@bigfoot.com>
Thu, 20 Jul 2023 13:42:40 +0000 (15:42 +0200)
post_to_main_thread.h [new file with mode: 0644]
video_widget.cpp

diff --git a/post_to_main_thread.h b/post_to_main_thread.h
new file mode 100644 (file)
index 0000000..080ed51
--- /dev/null
@@ -0,0 +1,27 @@
+#ifndef _POST_TO_MAIN_THREAD_H
+#define _POST_TO_MAIN_THREAD_H 1
+
+#include <QApplication>
+#include <QObject>
+#include <future>
+#include <memory>
+
+// http://stackoverflow.com/questions/21646467/how-to-execute-a-functor-in-a-given-thread-in-qt-gcd-style
+template<typename F>
+static inline void post_to_main_thread(F &&fun)
+{
+       QObject signalSource;
+       QObject::connect(&signalSource, &QObject::destroyed, qApp, std::move(fun));
+}
+
+template<typename F>
+static inline void post_to_main_thread_and_wait(F &&fun)
+{
+       std::promise<void> done_promise;
+       std::future<void> done = done_promise.get_future();
+       post_to_main_thread(std::move(fun));
+       post_to_main_thread([&done_promise] { done_promise.set_value(); });
+       done.wait();
+}
+
+#endif  // !defined(_POST_TO_MAIN_THREAD_H)
index a5b45136537552282d1ec8c118f5cd998de51adb..126e874aa53b1591e6b53d984a26cfd63a25ad1e 100644 (file)
@@ -30,6 +30,8 @@ extern "C" {
 #include <vector>
 #include <unordered_set>
 
+#include "post_to_main_thread.h"
+
 #include <QOpenGLFunctions>
 #include <QWheelEvent>
 #include <QMouseEvent>
@@ -946,7 +948,9 @@ void VideoWidget::store_pts(int64_t pts)
 {
        last_pts = pts;
        last_position = lrint(pts * double(video_timebase.num) / double(video_timebase.den) * 1000);
-       emit position_changed(last_position);
+       post_to_main_thread([this, last_position{last_position.load()}] {
+               emit position_changed(last_position);
+       });
 }
 
 // Taken from Movit (see the comment there for explanation)