gpt4 book ai didi

c++ - QThreads 的并发问题。接收相同信号的线程相互阻塞

转载 作者:塔克拉玛干 更新时间:2023-11-03 02:05:44 27 4
gpt4 key购买 nike

所以我正在开发一个实时处理视频的程序,但我在线程相互“阻塞”方面遇到了一些麻烦。

我的系统几乎是这样设置的:

         DataSourceThread
/ \
/ \
/ \
Receiver Receiver
/ \
/ \
/ \
Processor1 Processor2

(所有这些类都扩展了 QThread。)

因此 DataSourceThread 从视频流中获取帧并向接收器发出包含该帧的信号。 连接类型:Qt::DirectConnection

接收器基本上接收由 DataSourceThread 发出的帧,如果处理器处理完前一帧,它将向处理器发出包含该帧的信号。 连接类型:Qt::QueuedConnection。如果处理器没有完成前一帧的处理,它只会返回而不发出信号(跳帧)。

为了测试这是否有效,我所做的只是让 Processor1 在收到帧时打印出一条消息,而 Processor2 执行 QThread::sleep(3); 并打印出一条消息.

(在将帧传递给处理器之前,接收方还将对帧进行深度复制。)

预期结果:

Processor1 应该不断打印信息。Processor2 应该每 3 秒打印一条消息。

问题:

两个处理器同时打印它们的消息(每 3 秒)。Processor1 等到 Processor2 完成后再打印消息。所以输出很像这样:

"Message from processor1"
"Message from processor2"
"Message from processor1"
"Message from processor2"
"Message from processor1"
"Message from processor2"

等等。

我的想法已经用完了,所以任何帮助将不胜感激!

编辑:下面是一些代码:

ma​​in.cpp:

int main(int argc, char *argv[])
{
QApplication app(argc, argv);

DataSourceThread dataSourceThread;
dataSourceThread.start();

GUIThread *guiProcessor = new GUIThread();
FrameReceiver *guiReceiver = new FrameReceiver(guiProcessor, 0);

QObject::connect(
&dataSourceThread, SIGNAL(frameReceived(Frame*)),
guiReceiver, SLOT(receive(Frame*)),
Qt::DirectConnection
);

DetectorThread *detectorProcessor = new DetectorThread();
FrameReceiver *detectorReceiver = new FrameReceiver(detectorProcessor, 0);

QObject::connect(
&dataSourceThread, SIGNAL(frameReceived(Frame*)),
detectorReceiver, SLOT(receive(Frame*)),
Qt::DirectConnection
);

return app.exec();
}

来自 DataSourceThread.cpp:

void DataSourceThread::run()
{
... stuff ...

while (true) {
image = cvQueryFrame(capture);

if (!image) {
qDebug() << QString("Could not capture frame");
continue;
}

cvReleaseImage(&temp_image);
temp_image = cvCreateImage(cvSize(640, 480), image->depth, 3);

cvResize(image, temp_image, 1);

frame->lock();
frame->setImage(temp_image);
frame->unlock();

emit frameReceived(frame);

msleep(1);
}
}

FrameReceiver.cpp:

FrameReceiver::FrameReceiver(FrameProcessor* processor, QObject *parent) : QThread(parent) {
m_ready = true;

m_processor = processor;
m_processor->start();

QObject::connect(
(QObject*)this, SIGNAL(frameReceived(Frame*)),
m_processor, SLOT(receive(Frame*)),
Qt::QueuedConnection
);

QObject::connect(
m_processor, SIGNAL(ready()),
(QObject*)this, SLOT(processCompleted()),
Qt::DirectConnection
); }

void FrameReceiver::processCompleted() {
m_ready = true; }

void FrameReceiver::receive(Frame *frame) {
if (m_ready == true) {
m_ready = false;
frame->lock();
Frame *f = new Frame(*frame);
frame->unlock();
emit frameReceived(f);
} else {
// SKIPPED THIS FRAME
}
}

GUIThread.cpp: (Processor1)

GUIThread::GUIThread(QObject *parent) : FrameProcessor(parent)
{
m_frame = new Frame();
}

void GUIThread::setFrame(Frame *frame)
{
qDebug() << QString("Guithread received frame");
}

FrameProcessor.cpp

// (The processors extend this class)
void FrameProcessor::receive(Frame *frame)
{
setFrame(frame);
delete frame;
emit ready();
}

DetectorThread (Processor2) 与 guithread 的功能相同,但在 setFrame 中休眠 3 秒。

最佳答案

我认为部分问题在于您的所有 QObject 都属于主应用程序线程。这意味着它们都共享一个事件循环来传递异步信号,从而有效地序列化整个处理链。

我认为正确的设置方式应该是这样的:

GUIProcessor *guiProcessor = new GUIProcessor();
QThread guiProcessorThread;
guiProcessor.moveToThread(&guiProcessorThread);

FrameReceiver *guiReceiver = new FrameReceiver(guiProcessor, 0);
QThread guiReceiverThread;
guiReceiver.moveToThread(&guiReceiverThread);

guiProcessorThread.start();
guiReceiverThread.start();

如果你这样做,我建议不要在线程之间使用 DirectConnection,而是使用 BlockingQueuedConnection 如果你想确保在捕获之前处理当前帧下一个。

看这个:http://labs.qt.nokia.com/2010/06/17/youre-doing-it-wrong/

还有这个:http://labs.qt.nokia.com/2006/12/04/threading-without-the-headache/

希望这对您有所帮助!

编辑:明确地说,根据我的建议,您的类将继承 QObject 而不是 QThread。

关于c++ - QThreads 的并发问题。接收相同信号的线程相互阻塞,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4727779/

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