Results 1 to 4 of 4

Thread: Problem stopping a thread

  1. #1
    Join Date
    Aug 2008
    Posts
    29
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Problem stopping a thread

    I have an administrative thread that starts several worker threads and collates the data they produce, and I am having trouble stopping it.

    Its run() loop basically looks like this:
    void run()
    {
    runflag = true;
    while(runflag)
    {
    take_buffers( in_fifo );
    nw = start_workers(); // nw is number of active workers
    exec(); // run event loop
    put_buffers( out_fifo);
    }
    }

    The event handler simply counts remaining workers:
    void worker_done()
    {
    --nw;
    if(nw <= 0) exit(); // break event loop
    }

    The stop() routine, to be called of course from other threads, looks something like this:
    bool stop()
    {
    runflag = false;
    in_fifo.flush();
    out_fifo.flush();
    return wait(500);
    }
    The fifo flush routines will unblock any thread waiting on a fifo operation.

    Problem is, stop() is always returning false, that is, the thread did not stop, and it appears that the reason is that it is stuck in its event loop. However, the thread does stop spontaneously shortly after stop() returns. So I am wondering, does QThread::wait() somehow stop the thread's event processing, or prevent the delivery of queued signals? Or does anyone know of another reason for this odd behavior?

    Qt 4.8(MinGW) on Windows Vista.

  2. #2
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,359
    Thanks
    3
    Thanked 5,015 Times in 4,792 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: Problem stopping a thread

    How do you actually use this code?
    Your biological and technological distinctiveness will be added to our own. Resistance is futile.

    Please ask Qt related questions on the forum and not using private messages or visitor messages.


  3. #3
    Join Date
    Aug 2008
    Posts
    29
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: Problem stopping a thread

    Solved. I was issuing the connect calls for the workers' signals in the same thread that would eventually call stop(), so they were in fact blocked by the wait(). Now I connect in the administrative thread, the signals are delivered to its event loop, and all works as expected.

    Wysota, try to think abstractly. Of course I don't "actually use" pseudo code.

  4. #4
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,359
    Thanks
    3
    Thanked 5,015 Times in 4,792 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: Problem stopping a thread

    Quote Originally Posted by tksharpless View Post
    Solved. I was issuing the connect calls for the workers' signals in the same thread that would eventually call stop(), so they were in fact blocked by the wait().
    That was my impression, that's why I asked how you used this code. Source code for a function is meaningless without a calling context.
    Your biological and technological distinctiveness will be added to our own. Resistance is futile.

    Please ask Qt related questions on the forum and not using private messages or visitor messages.


Similar Threads

  1. stopping qprocess from QDialog
    By mohit_king in forum Newbie
    Replies: 5
    Last Post: 14th February 2011, 07:56
  2. Stopping a QThread
    By Gurdt in forum Qt Programming
    Replies: 4
    Last Post: 23rd April 2009, 22:21
  3. Stopping QTimer
    By drinu21 in forum Qt Programming
    Replies: 4
    Last Post: 16th May 2008, 15:59
  4. thread implementing and stopping the thread
    By santosh.kumar in forum Qt Programming
    Replies: 1
    Last Post: 14th May 2007, 13:53
  5. stopping non-singlesshot QTimer
    By quickNitin in forum Newbie
    Replies: 1
    Last Post: 15th June 2006, 10:36

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.