Solved Multithread with dll run much more slowlier in QThread
-
Hi, I'm using QThread with a cam application,msvc with qt5.9,
basically it's capturing frame with Opencv and emit cropped frames to a preprocessing thread.However, I've found that time of function inside my preprocess dll is almost being doubled comparing to my own C++ application using same dll.
(about 100-200ms comparing 40ms 1 loop for same function)
I've traced in and found that every function inside this function is costing more time.
I believe there's something wrong with my code but I' ve searched long and still get no answers.
Here's my guess:
- I'm initializing my dll's class in wrong position, I've put dll class's intialize (ocr_api *ocr_a and new) inside ocr_thread::ocr_thread(),
found it was actually inside main thread, but this is not fixing the problem(Maybe a little, not sure)
Is it good to initialize inside ocr_thread.hpp?
2.There's something wrong with my QThread, although I've checked the Internet to make sure I'm using the proper way.
3.Something inside main thread is delaying my thread, like setpixmap in loop and mattoqimage function (But I'm now using 4core cpu, I guess there shouldn't be delayed while using Qtimer between threads?)
Maybe it's a noob question but I'm still expecting some advice, Thanks in advance!
Here's how it looks like.
app0.cpp(QThread thread2;) ocr_use = new Ocr_thread(); ocr_use->moveToThread(&thread2); timer = new QTimer(this); connect(timer, SIGNAL(timeout()), this, SLOT(cropimg())); void updateroi(QRectF) //slot to emit cropimg signals. { thread2.start(); timer->setInterval(1000);//I'm sleeping 1000ms here to avoid duplicate of signals emit timer->start(); } void app0::cropimg() { image = image_update.copy(ori_x,ori_y, ori_width, ori_height);//videoviewer2 if(image.isNull()==false){ emit sendcropimage(image); ui->videoviewer2->setPixmap(QPixmap::fromImage(image)); } }
ocr_thread.h
#include "ocr_d.h" #pragma comment(lib,"ocr_d.lib") class Ocr_thread :public QObject { Q_OBJECT private: ocr_api ocr_a; //my dll class, using dllimport public slots: void getcropimage(QImage); bool rec(Mat _frame);
ocr_thread.cpp
void getcropimage(QImage) { Mat matin = QImagetocvMat(crop); bool a = rec(matin); } bool Ocr_thread::rec(Mat _frame) { clock_t start, end; start = clock(); ocr_a.rec(input, digits, softmax, output, canny_crop, rect_crop, threshimg); //the time of this function of dll which is being doubled here. end = clock(); double endtime = (double)(end - start) / CLOCKS_PER_SEC; }
- I'm initializing my dll's class in wrong position, I've put dll class's intialize (ocr_api *ocr_a and new) inside ocr_thread::ocr_thread(),
-
Hi,
From a quick look, you are likely losing time due to a lot of useless copies of your data.
Also, is your external dlls doing some locking ?
Why exactly do you need to sleep ?
-
Hi,
I believe I'm losing time due to any function from dll, I'm measuring time by clock_t above and copying time are not included.
It's a strange thing and I'm trying to look for advice and possible reasons that could slow down the whole program with a dll. (Not sure if 3 of my guess above effecting the program)
It's not using anything could lock to build it, variables are all getting another copied inside it.Here's my guess:
1.I'm initializing my dll's class in wrong position, I've put dll class's intialize (ocr_api *ocr_a and new) inside ocr_thread::ocr_thread(),
found it was actually inside main thread, but this is not fixing the problem(Maybe a little, not sure)
Is it good to initialize inside ocr_thread.hpp?
2.There's something wrong with my QThread, although I've checked the Internet to make sure I'm using the proper way.3.Something inside main thread is delaying my thread, like setpixmap in loop and mattoqimage function (But I'm now using 4core cpu, I guess there shouldn't be delayed while using Qtimer between threads?)
-
Did you try to run your application through a performance analyzer ?
-
@SGaist Yes I did, there's only 20% occupying of cpu usage with my own application and maximum to 50%, I believe it's my Qt code problem, but not dll's.
Is it possible that queue to transmit pointer of frame behave better than just sending signals and slots, I'm doubting the slots with movetothread are slow. -
@Puppy-Bear said in Multithread with dll run much more slowlier in QThread:
I'm doubting the slots with movetothread are slow.
No
-
This post is deleted! -
Just found out that I'm not using O2 with release which cause 80%delayed
and 20%is caused by my Qt programming's clone img.