Multithreaded advice

I am developing an application, the main goal is to capture images from the frame invader, do some processing, and then show the images in a graphical interface. The cooling tower is connected to PCIe. And I use the SDK to capture frames. Image flow is rather slow from 10 to 100 images / s. I am here to get some tips about my code and how to optimize it. Firstly, there is my run () function from a class inherited from Qthread. I grab the image and put it in the queuecv :: Mat> buffer.

void ImageIn::run(){ _cam->allocMemory(); _cam->startAquisition(); _runningThread = true; while(_runningThread) { Mat image(_cam.getSizeX(), _cam.getSizeY(), CV_16U, _cam->getImageDMA0()); _ctrl->getMutexIn()->lock(); // Lock BufferIn _ctrl->getBufferIn()->push(image); // store Image in BufferIn _ctrl->getMutexIn()->unlock(); // Unlock bufferIn } } 

Images are stored in a buffer, and then the processing thread does some work ...

 void ImageProcessing::run(){ while(_runningThread){ if (_ctrl->getMutexIn()->tryLock()){ while(!_ctrl->getBufferIn()->empty()){ _ctrl->getBufferIn()->front().convertTo(tempConvert, CV_32F); _bufferLocalIn.push(tempConvert); _ctrl->getBufferIn()->pop(); } _ctrl->getMutexIn()->unlock(); } // Do some processing and put image and a buffer for GUI } } 

So, I have a few questions: - Stream 1 receives images thanks to the lock function, so the processor consumption is low, but stream 2 runs continuously and consumes a lot of processor reevaluation, what can I do to fix it? - Is this the correct code?


So I tried:

 QThread* thread = new QThread; ImageWriter* worker = new ImageWriter(); worker->moveToThread(thread); QTimer* timer = new QTimer(); int msec = 100; timer->setInterval(msec); QObject::connect(thread, SIGNAL(started()), worker, SLOT(process())); QObject::connect(worker, SIGNAL(finished()), thread, SLOT(quit())); QObject::connect(timer, SIGNAL(timeout()), thread, SLOT(start())); timer->start(); 

this is normal?

+5
source share
1 answer

Since Qt is an event-driven environment, each thread has an event queue that receives events and sends them to various objects to receive them.

If any section of code in the thread essentially has a while (1) loop, then event propagation cannot happen, and this is what you do in both threads with

 while(_runningThread) 

Fasting event processing in an infinite loop causes excessive CPU consumption.

You can use Qt to handle events with a call to QApplication :: processEvents in infinite loops, but this is not ideal.

The best method would be to reduce processing time with QTimer and allow the event to propagate naturally. This will require a decision on the processing time before storing the processing state and then returning to the event loop. A tick of the timer will call your processing function, which will then be able to restore the state and resume.

This method will be simpler if you create a processing class derived from QObject that you will migrate to QThread and not directly inherit from QThread itself. There's a good article on How to Really Use QThread , which you can use as a template for how to do this.

Finally, keep in mind that you can have more than one QThread , as well as more than one QObject running on QThread . As a rule, you will not win if the number of threads exceeds the number of processor cores available.

If you know that the target machine is a quad core, you can create 3 additional QThreads (total 4, including the main one), create several processing objects and move them to different threads to ensure optimal processing.

+2
source

Source: https://habr.com/ru/post/1237659/


All Articles