Results 1 to 3 of 3

Thread: Calling QThread::exec() repeatedly

  1. #1
    Join Date
    May 2007
    Posts
    19
    Thanks
    5
    Thanked 1 Time in 1 Post
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Calling QThread::exec() repeatedly

    I have a problem with the event loop in a QThread. Say I have a class "Player" that inherits QThread. In the run() function, I have an infinite loop where the thread is put to sleep. Once it's woken up, it can do different things depending on the state of the class. One possibility is to call member functions of another class "Playback", which makes use of a QTimer, so I need to call the exec() function of the thread. Once the Playback's timer is finished with its stuff, it emits a signal that is caught by the thread class, which then calls quit() to get out of the event loop, and puts the thread to sleep again. This works very well when it is called the first time. On the second time, exec() returns immediately and no event loop is entered.

    Is it possible to call exec() of a QThread repeatedly (but not concurrently)? In other words, can the following calling sequence be modified to work in a QThread class?:
    exec() , quit(), exec()

    Maybe by somehow "resetting" the event loop after the quit()?

    Excerpt from the code:

    Qt Code:
    1. // Player inherits QThread
    2. void Player::run()
    3. {
    4. mutex.lock();
    5. playback = new Playback();
    6. QObject::connect(playback, SIGNAL(finishedPlayback()), this, SLOT(done()));
    7. mutex.unlock();
    8.  
    9. while(1) {
    10. mutex.lock();
    11. cond.wait(&mutex);
    12. mutex.unlock();
    13.  
    14. if(abort) {
    15. mutex.lock();
    16. delete playback;
    17. playback = NULL;
    18. mutex.unlock();
    19. return;
    20. }
    21.  
    22. if(status == Forward) {
    23. // This call will start a QTimer, and emit finishedPlayback() when done
    24. playback->forward(startTimestamp, endTimestamp, timestep);
    25. exec();
    26. }
    27. }
    28. }
    29.  
    30. void Player::done(void)
    31. {
    32. mutex.lock();
    33. status = Finished;
    34. mutex.unlock();
    35. quit();
    36. }
    To copy to clipboard, switch view to plain text mode 

  2. #2
    Join Date
    Feb 2006
    Location
    Oslo, Norway
    Posts
    6,264
    Thanks
    36
    Thanked 1,519 Times in 1,389 Posts
    Qt products
    Qt4
    Platforms
    MacOS X Unix/X11 Windows Symbian S60 Maemo/MeeGo

    Default Re: Calling QThread::exec() repeatedly

    This bug has been fixed in Qt 4.3.0: 140734 - QThread, cant start exec more than once
    J-P Nurmi

  3. The following user says thank you to jpn for this useful post:

    hb (26th June 2007)

  4. #3
    Join Date
    May 2007
    Posts
    19
    Thanks
    5
    Thanked 1 Time in 1 Post
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: Calling QThread::exec() repeatedly

    Thank you for this information! Thinking the error was on my side, I've searched everywhere except bug trackers...

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Digia, Qt and their respective logos are trademarks of Digia Plc in Finland and/or other countries worldwide.