gpt4 book ai didi

c++ - Qt:向死线程/停止线程发送信号

转载 作者:行者123 更新时间:2023-11-28 01:31:37 25 4
gpt4 key购买 nike

我遇到了这个问题,我可能正在从一个 QThread 向另一个发送信号,但是我无法检查线程是否正在实现 slot 正在运行。在使用 Qt::QueuedConnection 时,框架将如何处理这种情况?

...
WorkerImp* pWorker = new WorkerImp();
QThread worker;
pWorker->moveToThread(&worker);

QObject::connect(&worker, QThread::finished, pWorker, &QObject::deleteLater, Qt::QueuedConnection);
bool connected = QObject::connect(this, &SlaveMaster::requireWork, pWorker, &WorkerImp::doWork, Qt::QueuedConnection);
assert(connected);

// at this point we have connected the signal, thread is not starded.
// however the object that we use to connect still exists and will after we exit the thread.


worker.start();
worker.exit();
worker.wait();

// note that when we exit the QThread we do not destroy the object - it can be start over

emit requireWork();
...

最佳答案

信号永远不会被处理。跨线程的排队连接作为事件发布到线程,由其事件循环处理。如果线程停止(因此它的事件循环),则没有人接收事件并传递它:

来自 Qt 文档 Signals and Slots Across Threads :

Queued Connection: The slot is invoked when control returns to the event loop of the receiver's thread. The slot is executed in the receiver's thread.

Per-Thread Event Loop :

[...] If no event loop is running, events won't be delivered to the object.

请注意,阻塞排队的连接会死锁

关于c++ - Qt:向死线程/停止线程发送信号,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51281578/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com