所以我正在开发一个实时处理视频的程序,而且我遇到了一些线程“阻塞”的问题。
我的系统设置如下:
DataSourceThread
/ \
/ \
/ \
Receiver Receiver
/ \
/ \
/ \
Processor1 Processor2
(所有这些类都在扩展QThread。)
因此,DataSourceThread从视频流中获取帧,并将包含该帧的信号发送到接收器。 连接类型:Qt :: DirectConnection
接收器基本上接收由DataSourceThread发出的帧,如果处理器完成处理前一帧,它将向处理器发出包含帧的信号。 连接类型:Qt :: QueuedConnection。 如果处理器没有完成处理前一帧,它将返回而不发出信号(跳过帧)。
为了测试这是否有效,我所做的就是让Processor1在接收到帧时打印出一条消息,而Processor2执行 QThread :: sleep(3); 并打印出一条消息
(接收器也会在将帧传递给处理器之前对帧进行深层复制。)
预期结果:
Processor1应该不断打印消息。 Processor2应该每隔3秒打印一条消息。
问题:
两个处理器同时打印他们的消息(每3秒)。 处理器1等待处理器2完成后再打印其消息。 所以输出就像这样:
"Message from processor1"
"Message from processor2"
"Message from processor1"
"Message from processor2"
"Message from processor1"
"Message from processor2"
等等。
我这里的想法已经不多了, 所以任何帮助将不胜感激!
修改 这是一些代码:
main.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 :(处理器1)
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秒。
答案 0 :(得分: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。