Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. Multithread with dll run much more slowlier in QThread
Forum Updated to NodeBB v4.3 + New Features

Multithread with dll run much more slowlier in QThread

Scheduled Pinned Locked Moved Solved General and Desktop
8 Posts 3 Posters 743 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • Puppy BearP Offline
    Puppy BearP Offline
    Puppy Bear
    wrote on last edited by Puppy Bear
    #1

    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:

    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?)

    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;
    }
    
    1 Reply Last reply
    0
    • SGaistS Offline
      SGaistS Offline
      SGaist
      Lifetime Qt Champion
      wrote on last edited by
      #2

      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 ?

      Interested in AI ? www.idiap.ch
      Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

      1 Reply Last reply
      0
      • Puppy BearP Offline
        Puppy BearP Offline
        Puppy Bear
        wrote on last edited by Puppy Bear
        #3

        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?)

        1 Reply Last reply
        0
        • SGaistS Offline
          SGaistS Offline
          SGaist
          Lifetime Qt Champion
          wrote on last edited by
          #4

          Did you try to run your application through a performance analyzer ?

          Interested in AI ? www.idiap.ch
          Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

          Puppy BearP 2 Replies Last reply
          0
          • SGaistS SGaist

            Did you try to run your application through a performance analyzer ?

            Puppy BearP Offline
            Puppy BearP Offline
            Puppy Bear
            wrote on last edited by Puppy Bear
            #5

            @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.

            Christian EhrlicherC 1 Reply Last reply
            0
            • Puppy BearP Puppy Bear

              @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.

              Christian EhrlicherC Offline
              Christian EhrlicherC Offline
              Christian Ehrlicher
              Lifetime Qt Champion
              wrote on last edited by
              #6

              @Puppy-Bear said in Multithread with dll run much more slowlier in QThread:

              I'm doubting the slots with movetothread are slow.

              No

              Qt Online Installer direct download: https://download.qt.io/official_releases/online_installers/
              Visit the Qt Academy at https://academy.qt.io/catalog

              1 Reply Last reply
              0
              • SGaistS SGaist

                Did you try to run your application through a performance analyzer ?

                Puppy BearP Offline
                Puppy BearP Offline
                Puppy Bear
                wrote on last edited by
                #7
                This post is deleted!
                1 Reply Last reply
                0
                • Puppy BearP Offline
                  Puppy BearP Offline
                  Puppy Bear
                  wrote on last edited by
                  #8

                  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.

                  1 Reply Last reply
                  0

                  • Login

                  • Login or register to search.
                  • First post
                    Last post
                  0
                  • Categories
                  • Recent
                  • Tags
                  • Popular
                  • Users
                  • Groups
                  • Search
                  • Get Qt Extensions
                  • Unsolved