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. QThread - QThread::wait() does not terminate
Qt 6.11 is out! See what's new in the release blog

QThread - QThread::wait() does not terminate

Scheduled Pinned Locked Moved Unsolved General and Desktop
4 Posts 4 Posters 844 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.
  • S Offline
    S Offline
    strenkwalder
    wrote on last edited by
    #1

    I have a fairly simple QThread class that causes me quite some headaches. I assume it is something super obvious that I'm just missing.
    I'm using Qt 5.8 on Ubuntu with normal gcc toolchain.

    I have a simple OpenCV video feed capturing QThread:

    class VideoCaptureThread : public QThread
    {
        bool terminating;
        int id;
        VideoBuffer *buffer;
    
    public:
        VideoCaptureThread(int camID = 0, VideoBuffer *b = 0);
        virtual ~VideoCaptureThread();
    
        // QThread interface
    protected:
        void run() override;
    
    public slots:
        void stopCapuring();
        void setID(int);
        void setBuffer(VideoBuffer *);
    };
    

    with:

    VideoCaptureThread::VideoCaptureThread(int camID, VideoBuffer *b) :
        terminating(false),
        id(camID),
        buffer(b)
    {}
    
    VideoCaptureThread::~VideoCaptureThread(){
        cv::destroyWindow("CaptureThread Test");
        terminating = true;
    
        this->quit(); //I tried exit(0) or terminate() as well
        this->wait(); //Wait always blocks
    }
    
    void VideoCaptureThread::run(){
    
        //This all works fine
        cv::VideoCapture video(id);
        cv::Mat frame;
    
        while(!terminating && video.isOpened() && buffer != 0){
    
            video >> frame;
    
            cv::imshow("CaptureThread Test",frame);
    
            buffer->addFrame(frame);
            qDebug() << buffer->size();
    
            QCoreApplication::processEvents();
        }
    }
    
    void VideoCaptureThread::stopCapuring(){ terminating = true; }
    void VideoCaptureThread::setID(int i){ id = i; }
    void VideoCaptureThread::setBuffer(VideoBuffer *b){ buffer = b; }
    

    I call this by:

        VideoCaptureThread *capThread = new VideoCaptureThread( 0, &vBuffer);
        connect(this,SIGNAL(stopCapturing()), capThread, SLOT(deleteLater()));
    
        //alternatively I tried -> (but this caused SigSeg .. in my main() -> a.exec() <--- QApplication
        //connect(this,SIGNAL(stopCapturing()), capThread, SLOT(stopCaputing()));
        //connect(capThread,SIGNAL(finished()),capThread,SLOT(deleteLater()));
        
        capThread->start(QThread::HighestPriority);
    

    When I use all this, my application gets stuck at ~VideoCaptureThread (this.wait()).
    This is such a simple thread. But I still can't see the problem.

    I would appreciate your help.

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

      Hi,

      Quit will only work if run start's the thread event loop which is not your case.

      You are using a loop so you have to exit that loop for your thread to end which you do in the second line of your destructor.

      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
      2
      • Gojir4G Offline
        Gojir4G Offline
        Gojir4
        wrote on last edited by
        #3
        This post is deleted!
        1 Reply Last reply
        0
        • VRoninV Offline
          VRoninV Offline
          VRonin
          wrote on last edited by VRonin
          #4

          Please see this thread from that point onward. You fell in the same misconceptions about QThread as that user. In your case you even have things like void VideoCaptureThread::setBuffer(VideoBuffer *b){ buffer = b; } as public which is a ticking time bomb.

          P.S.
          cv::imshow creates a window if I'm not mistaken and it should not be done in a secondary thread (on Windows your app would crash straight away)

          "La mort n'est rien, mais vivre vaincu et sans gloire, c'est mourir tous les jours"
          ~Napoleon Bonaparte

          On a crusade to banish setIndexWidget() from the holy land of Qt

          1 Reply Last reply
          1

          • Login

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