qt信号时隙多线程死锁

编程入门 行业动态 更新时间:2024-10-28 18:25:28
本文介绍了qt信号时隙多线程死锁的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

在我的代码中,工作线程向gui线程发出一个信号。信号和插槽通过Qt :: BlockingQueuedConnection连接。

In my code a worker thread emits a signal to gui thread. Signal and Slot are connected via Qt::BlockingQueuedConnection.

当 closeEvent 应用程序尝试停止工作

When during thecloseEvent application is trying to stop the worker thread and only then finishes the event.

它调用工作线程的函数:

It calls the function of worker thread:

void stop() { _isFinished = true; wait(); }

工作线程检查 _isFinished <

The worker thread in its turn checks _isFinished condition and if it is not set up then emit the signal.

成像以下情况

The worker Gui Thread ================= ================= if (!_isFinished) ----------------- ----------------- _isFinished = true; ----------------- wait(); emit mySignal(); -----------------

显然,将被锁定 - 死锁。

Evidently, both threads would be locked - deadlock.

如果我添加一个互斥,那么你可以找到另一个死锁情况。当工作者锁定互斥量和gui线程将被阻塞。结果发出的信号将锁定工人。

If I add a mutex then you can find another deadlock situation. When the worker locks the mutex and the gui thread would be blocked. As a result the emitted signal will lock the worker.

我该如何处理?

推荐答案

这是我看到的情况。

主线程你不需要等待工人退出。您的 closeEvent 应该看起来像这样:

In your main thread you do not need to wait for worker to quit. Instead your closeEvent should look like this:

if (workerThread->isRunning()) { workerThread->stop(); event->ignore(); }

要使这项工作,你还需要关闭主窗口,完成:

To make this work, you also need to close main window, when worker thread finishes:

connect(workerThread, SIGNAL(finished()), SLOT(close()));

最后,使用 _isFinished 所有其他变量在工作线程)应该发生在工作线程。您可以用一个简单的技巧实现它:

Finally, all manipulations with _isFinished (and all other variables in worker thread) should occur in worker thread. You can achieve it with a simple trick:

WorkerThread.h

public: void stop(); protected slots: void p_stop(); protected: QMutex mutex;

WorkerThread.cpp

void WorkerThread::stop() { staticMetaObject.invokeMethod(this, "p_stop", Qt::QueuedConnection); } void WorkerThread::p_stop(); { QMutexLocker locker(&mutex); _isFinished = true; }

这样可以避免死锁。

更多推荐

qt信号时隙多线程死锁

本文发布于:2023-10-24 07:28:30,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1523322.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:死锁   多线程   信号   qt

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!