Results 1 to 9 of 9

Thread: My thread for non-blocking stdin input keeps running after program terminates

  1. #1
    Join Date
    Mar 2010
    Posts
    5
    Thanked 1 Time in 1 Post
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default My thread for non-blocking stdin input keeps running after program terminates

    Hello, I am new in this forum, and I am very enthusiastic about Qt.
    However, I have the following problem. Since I understand that there is no built-in facility to check for input on stdin, I wrote a thread for it. This works fine, but after program termination the error message
    "QThread: Destroyed while thread is still running" appears.
    In this thread I wrote this simple run function:

    void InputThread::run()
    { fgets(inputLine, 200, stdin);
    }

    Since the input data read from stdin, if available, starts with 'x', I test whether input is available as follows:

    if (inputThread->inputLine[0] == 'x') ... // input available in inputLine

    It looks like, due to the fgets input command, the thread keeps running, despite my attempts to stop it by calling inputThread.exit() or inputThread.quit.
    Can anyone please help? Any solution for checking if stdin input is available is welcome, provided it works on both Linux and Windows.

  2. #2
    Join Date
    Mar 2008
    Location
    France
    Posts
    149
    Thanks
    2
    Thanked 21 Times in 21 Posts
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: My thread for non-blocking stdin input keeps running after program terminates

    To finish a thread properly in Qt, use a flag inside the run() function when you want to make it stop.
    You set this flag from outside the thread that is checked by the computation within the thread and stop the calculation if the flag is set.

    Qt Code:
    1. void myThread::run()
    2. {
    3. while (! isThreadstopped)
    4. {
    5. // do your threaded stuff;
    6. }
    7.  
    8. } // thread has terminated.
    To copy to clipboard, switch view to plain text mode 

  3. #3
    Join Date
    Mar 2010
    Posts
    5
    Thanked 1 Time in 1 Post
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: My thread for non-blocking stdin input keeps running after program terminates

    Thank you for your attempt to help me, toutarrive, but this does not work. The point is that, in the case there is no input data available on stdin, the thread keeps waiting for this input data (which may never appear).
    Therefore, if there will be no input data, this test "while (!isThreadstopped) of yours is done only once. I nevertheless tried this loop, but, as I expected, it did not help.

  4. #4
    Join Date
    Mar 2008
    Location
    France
    Posts
    149
    Thanks
    2
    Thanked 21 Times in 21 Posts
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: My thread for non-blocking stdin input keeps running after program terminates

    Whenever you want to stop the thread, send a signal to the thread and set the flag ( isThreadstopped to true, member of your thread class ) in the signal handler.

  5. #5
    Join Date
    Oct 2009
    Posts
    151
    Thanks
    6
    Thanked 13 Times in 11 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: My thread for non-blocking stdin input keeps running after program terminates

    Try putting an exec(); statement after the fgets().

    inputThread.exit() should then work.

  6. #6
    Join Date
    Mar 2010
    Posts
    5
    Thanked 1 Time in 1 Post
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: My thread for non-blocking stdin input keeps running after program terminates

    Thanks JD200, but the point is that any statement immediately following fgets() is never executed if there is no input available on stdin.
    I nevertheless tried your suggestion (inserting exec()), but without success.

  7. #7
    Join Date
    Mar 2008
    Location
    Kraków, Poland
    Posts
    1,536
    Thanked 284 Times in 279 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: My thread for non-blocking stdin input keeps running after program terminates

    So maybe You can not to use fgets() ?

  8. #8
    Join Date
    Jan 2009
    Location
    The Netherlands and Spain
    Posts
    150
    Thanks
    6
    Thanked 18 Times in 18 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default Re: My thread for non-blocking stdin input keeps running after program terminates

    You can try to use an external process to read user input. In Linux that would/could be "read".
    At program start you create a QProcess and connect its signals to your MainWindow, so you can read the users input.
    When quiting the application, you terminate -close- the QProcess.

    Hope this helps...

  9. #9
    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: My thread for non-blocking stdin input keeps running after program terminates

    Did you try using QSocketNotifier with STDIN as the socket descriptor? You don't need a thread for that...
    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. Non-Gui thread blocking gui
    By Valheru in forum Qt Programming
    Replies: 17
    Last Post: 12th February 2010, 11:11
  2. Best thread non-blocking technics
    By Tanuki-no Torigava in forum Qt Programming
    Replies: 0
    Last Post: 8th December 2009, 15:53
  3. Looking to NOT echo input when writing to stdin
    By ultim8 in forum Qt Programming
    Replies: 1
    Last Post: 19th June 2009, 21:57
  4. IPC / reading stdin within non-GUI thread
    By mule in forum Qt Programming
    Replies: 3
    Last Post: 29th November 2007, 22:11
  5. Replies: 1
    Last Post: 17th May 2006, 00:23

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.