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. Threads problem
QtWS25 Last Chance

Threads problem

Scheduled Pinned Locked Moved General and Desktop
17 Posts 8 Posters 6.5k Views
  • 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.
  • H Offline
    H Offline
    Hornsj2
    wrote on last edited by
    #3

    Forgive my ignorance here but this is an opportunity to learn something about Qt. I only write GUI applications and usually there is an application.show() which starts an event loop.

    Does the above example cover non-GUI applications?

    1 Reply Last reply
    0
    • S Offline
      S Offline
      skypjack
      wrote on last edited by
      #4

      QCoreApplication. That's all.

      1 Reply Last reply
      0
      • M Offline
        M Offline
        MAMartins
        wrote on last edited by
        #5

        [quote author="Gerolf" date="1302635887"]Your problem is the main functions:

        @
        int main(int argc, char *argv[])
        {

        faceTracker * ft = new faceTracker;
        ft->run(); // This is the version with no thread, to have thread just put it ft->start();
        QApplication app(argc, argv); // <-- this is no wait!
        

        }
        @

        you start your thread and then exit main. exiting main is exiting your application which means, th eprocess is exited and all threads are killed.

        Try the following code:

        @
        int main(int argc, char *argv[])
        {
        QApplication app(argc, argv); // <-- this is no wait!

        faceTracker * ft = new faceTracker;
        ft->start();
        ft->wait();
        

        }
        @
        [/quote]
        thanks a lot Gerolf! that solved my problem! I just forgot that the wait is also to make the main thread wait for the other, i was thinking that ft->wait() will put ft on hold

        can you explain me why now this app with no wait works good?

        code:
        @
        #include "thread.h"
        #include <cassert>
        #include <iostream>
        #include <QHash>
        #include <QDir>
        #include <QDebug>
        #include <QThread>

        thread::thread(){

        }

        void thread::run(){
        int i = 0;

        while(i < 1000000){
            printf("%d \n", i);
            i++;
        }
        

        }
        @

        main:
        @
        #include <QtGui/QApplication>
        #include "thread.h"
        #include <QList>
        #include <QDebug>
        #include <QObject>
        #include <QWidget>
        #include <QGraphicsObject>
        #include <QState>
        #include <QGraphicsObject>

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

        thread * ft = new thread;
        ft->start();
        
        
        return a.exec&#40;&#41;;
        

        }
        @

        as far as I see the apps are the same :o

        1 Reply Last reply
        0
        • M Offline
          M Offline
          MAMartins
          wrote on last edited by
          #6

          Now I'm trying use a thread on the openCV and my program is closing with no specific error in the crash report i have this:
          @

          Thread 7 Crashed:
          0 OpenCV 0x0001209a GOMP_parallel_end + 58
          1 OpenCV 0x00012121 GOMP_parallel_start + 17
          2 OpenCV 0x00335108 cvSetImagesForHaarClassifierCascade + 1560
          3 ??? 0x0000002f 0 + 47
          @

          my code:
          @
          faceTracker::faceTracker()
          {
          qDebug("teste1");
          filename = "/haarcascades/haarcascade_frontalface_alt_tree.xml";

          /* load the classifier
              note that I put the file in the same directory with this code */
          cascade = ( CvHaarClassifierCascade* )cvLoad( filename, 0, 0, 0 );
          if(!cascade){
              qDebug("cascade!");
              exit(0);
          }
          /* setup memory buffer; needed by the face detector */
          storage = cvCreateMemStorage( 0 );
          if(!storage){
              qDebug("storage");
              exit(0);
          }
          
          /* initialize camera */
          capture = cvCaptureFromCAM( 0 );
          if(!capture){
              qDebug("capture");
              exit(0);
          }
          
          /* always check */
          assert( cascade && storage && capture );
          

          }

          void faceTracker::detectFaces( IplImage img )
          {
          /
          detect faces */
          faces = cvHaarDetectObjects(
          img,
          cascade,
          storage,
          1.1,
          3,
          0 /CV_HAAR_DO_CANNY_PRUNNING/,
          cvSize( 40, 40 ) );

          /* for each face found, draw a red box */
          for( i = 0 ; i < ( faces ? faces->total : 0 ) ; i++ ) {
              CvRect *r = ( CvRect* )cvGetSeqElem( faces, i );
              cvRectangle( img,
                  cvPoint( r->x, r->y ),
                  cvPoint( r->x + r->width, r->y + r->height ),
                  CV_RGB( 255, 0, 0 ), 1, 8, 0 );
              qDebug("caras: %d", faces->total);
          }
          
          /* display video */
          cvShowImage( "video", img );
          

          }

          void faceTracker::run()
          {
          qDebug("teste2");

          while( key != 'q' ) {
              /* get a frame */
              frame = cvQueryFrame( capture );
              /* always check */
              if( !frame ) break;
          
              /* 'fix' frame */
              cvFlip( frame, frame, 1 );
          
              frame->origin = 0;
          
              /* detect faces and display video */
              detectFaces( frame );
          
          
              /* quit if user press 'q' */
              key = cvWaitKey( 10 );
          
          }
          
          /* free memory */
          cvReleaseCapture( &capture );
          cvDestroyWindow( "video" );
          cvReleaseHaarClassifierCascade( &cascade );
          cvReleaseMemStorage( &storage );
          

          }

          @

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

          app.setApplicationName( "DisplUM" );
          
          faceTracker * ft = new faceTracker;
          ft->start();
          ft->wait();
          

          }
          @

          1 Reply Last reply
          0
          • F Offline
            F Offline
            florent.revelut
            wrote on last edited by
            #7

            [quote author="MAMartins" date="1302640707"]
            main:
            @
            #include <QtGui/QApplication>
            #include "thread.h"
            #include <QList>
            #include <QDebug>
            #include <QObject>
            #include <QWidget>
            #include <QGraphicsObject>
            #include <QState>
            #include <QGraphicsObject>

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

            thread * ft = new thread;
            ft->start();
            
            
            return a.exec&#40;&#41;;
            

            }
            @

            as far as I see the apps are the same :o
            [/quote]

            The difference lies in your a.exec().
            In the previous one, you were creating the app on stack then exiting the main funciton, thus exiting program.
            In your second one, you tell the application to start the main thread, leaving time to switch to other threads.

            Most probably,your first application exited directly, without doing anything, while this one waits for you to close the window (or at least, doesn't end itself when processing is over).

            1 Reply Last reply
            0
            • F Offline
              F Offline
              florent.revelut
              wrote on last edited by
              #8

              [quote author="MAMartins" date="1302656456"]Now I'm trying use a thread on the openCV and my program is closing with no specific error in the crash report i have this:
              @

              Thread 7 Crashed:
              0 OpenCV 0x0001209a GOMP_parallel_end + 58
              1 OpenCV 0x00012121 GOMP_parallel_start + 17
              2 OpenCV 0x00335108 cvSetImagesForHaarClassifierCascade + 1560
              3 ??? 0x0000002f 0 + 47
              @
              [/quote]

              I'm not 100% sure, but it smells like NULL pointer dereferencement (0+47 : 0 is base pointer, 47 is offset, sum of them equals 02f, which is invalid address and crashed your program). Now, I definitely lack code to tell more.

              1 Reply Last reply
              0
              • G Offline
                G Offline
                giesbert
                wrote on last edited by
                #9

                [quote author="florent.revelut" date="1302673342"][quote author="MAMartins" date="1302640707"]
                main:
                @
                int main(int argc, char *argv[])
                {
                QApplication a(argc, argv);

                thread * ft = new thread;
                ft->start();
                
                
                return a.exec&#40;&#41;;
                

                }
                @

                as far as I see the apps are the same :o
                [/quote]

                The difference lies in your a.exec().
                In the previous one, you were creating the app on stack then exiting the main funciton, thus exiting program.
                In your second one, you tell the application to start the main thread, leaving time to switch to other threads.

                Most probably,your first application exited directly, without doing anything, while this one waits for you to close the window (or at least, doesn't end itself when processing is over).[/quote]

                a.exec() does not start the main thread, The main thread is started by the OS. a.exec() starts the eventLoop which is used to get UI and other events and deliver them to the corresponding objects. It ius running untill the loop is exited, e.g. by closing all top level windows, calling QApplication::quit, etc...

                Nokia Certified Qt Specialist.
                Programming Is Like Sex: One mistake and you have to support it for the rest of your life. (Michael Sinz)

                1 Reply Last reply
                0
                • G Offline
                  G Offline
                  giesbert
                  wrote on last edited by
                  #10

                  [quote author="florent.revelut" date="1302673593"][quote author="MAMartins" date="1302656456"]Now I'm trying use a thread on the openCV and my program is closing with no specific error in the crash report i have this:
                  @

                  Thread 7 Crashed:
                  0 OpenCV 0x0001209a GOMP_parallel_end + 58
                  1 OpenCV 0x00012121 GOMP_parallel_start + 17
                  2 OpenCV 0x00335108 cvSetImagesForHaarClassifierCascade + 1560
                  3 ??? 0x0000002f 0 + 47
                  @
                  [/quote]

                  I'm not 100% sure, but it smells like NULL pointer dereferencement (0+47 : 0 is base pointer, 47 is offset, sum of them equals 02f, which is invalid address and crashed your program). Now, I definitely lack code to tell more.[/quote]

                  I suggest you debug your app and step through the code of the thread. Then you will see which call crashes.

                  Nokia Certified Qt Specialist.
                  Programming Is Like Sex: One mistake and you have to support it for the rest of your life. (Michael Sinz)

                  1 Reply Last reply
                  0
                  • M Offline
                    M Offline
                    MAMartins
                    wrote on last edited by
                    #11

                    regarding the programs that do the sum, so the first lacked the a.exec()?

                    how can I debug my program properly? =/ this is weird because with no thread it runs good =/

                    with qdebug I know that the app runs until void faceTracker::detectFaces( IplImage *img ), but after faces = ... i have no qDebug in the console showing

                    1 Reply Last reply
                    0
                    • F Offline
                      F Offline
                      florent.revelut
                      wrote on last edited by
                      #12

                      [quote author="MAMartins" date="1302685551"]regarding the programs that do the sum, so the first lacked the a.exec()?
                      [/quote]

                      Yes

                      [quote author="MAMartins" date="1302685551"]
                      how can I debug my program properly? =/ this is weird because with no thread it runs good =/

                      with qdebug I know that the app runs until void faceTracker::detectFaces( IplImage *img ), but after faces = ... i have no qDebug in the console showing
                      [/quote]

                      Use an actual debugger (gdb, visual studio debugger), it allows to step and inspect stack, variables, memory.

                      If it works in single thread context and not in multithread, classical causes are:

                      • access to resources from more than one thread without protection (QMutex), leading to corrupted data
                      • race conditions between threads (once again, due to missing synchronization)

                      Edit: fixed list presentation. Please use * before a list item (or # for numbered lists), not a -; Andre

                      1 Reply Last reply
                      0
                      • G Offline
                        G Offline
                        giesbert
                        wrote on last edited by
                        #13

                        [quote author="MAMartins" date="1302685551"]regarding the programs that do the sum, so the first lacked the a.exec()?

                        how can I debug my program properly? =/ this is weird because with no thread it runs good =/
                        [/quote]

                        Use QtCreator or MSVS etc and set breakpoints.
                        If you need help on doing this, have a look at QtCreators docs. Usually it's by pressing "F9" on a line.

                        Then call F5 to start debugging.

                        Nokia Certified Qt Specialist.
                        Programming Is Like Sex: One mistake and you have to support it for the rest of your life. (Michael Sinz)

                        1 Reply Last reply
                        0
                        • A Offline
                          A Offline
                          azr79
                          wrote on last edited by
                          #14

                          This project is pretty interesting, I always wanted to know how face detect algorithms work. Let me know when finished ;)

                          Azr79

                          1 Reply Last reply
                          0
                          • M Offline
                            M Offline
                            MAMartins
                            wrote on last edited by
                            #15

                            [quote author="Gerolf" date="1302694209"]
                            [quote author="MAMartins" date="1302685551"]regarding the programs that do the sum, so the first lacked the a.exec()?

                            how can I debug my program properly? =/ this is weird because with no thread it runs good =/
                            [/quote]

                            Use QtCreator or MSVS etc and set breakpoints.
                            If you need help on doing this, have a look at QtCreators docs. Usually it's by pressing "F9" on a line.

                            Then call F5 to start debugging.

                            [/quote]
                            I'll try to find my error using the debugger, i'll keep you guys posted :)

                            1 Reply Last reply
                            0
                            • P Offline
                              P Offline
                              Polto
                              wrote on last edited by
                              #16

                              www.remnum.com

                              this is a video about building opencv libs on windows and mac os you can download it
                              with a project for opencv + qt using thread and it is run good try it

                              Polto

                              1 Reply Last reply
                              0
                              • W Offline
                                W Offline
                                webmaster.skelton
                                wrote on last edited by
                                #17

                                I do not know if this makes a difference or not(as it seems you are not using the signal/slot mechanism) but calling exec() within your run reimplementation allows the thread to run in its own event loop. This tends to prevent some memory allocation issues and allows for thread safe usage of signals and slots. Like i said I do not know if this will hold any relevance in your case.

                                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