Results 1 to 5 of 5

Thread: Is a QProcess thread safe in Qt4?

  1. #1
    Join Date
    Aug 2009
    Location
    Boston Area, USA
    Posts
    2
    Qt products
    Qt3 Qt4
    Platforms
    Unix/X11 Windows

    Default Is a QProcess thread safe in Qt4?

    At work we have a product that uses Qt3 and we are moving it to Qt4. In Qt3, you can't launch or even check isRunning() in another thread without risking a crash, though the crash is usually pretty rare.

    However, it appears Qt4 QProcesses have changed a good bit from 3 to 4 and some functions are even designed for separate threads.

    Does this mean I can start a program or check its state from another thread in Qt4 without risking a crash? Note that I can't use the functions that block until something happens as the separate thread handles the progress bar which needs to update every second or so.

  2. #2
    Join Date
    Dec 2007
    Posts
    628
    Thanks
    3
    Thanked 89 Times in 87 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    MacOS X Unix/X11 Windows

    Default Re: Is a QProcess thread safe in Qt4?

    Quote Originally Posted by Jay_D View Post
    Does this mean I can start a program or check its state from another thread in Qt4 without risking a crash?
    Yes no probs !
    You need to call this function.
    Qt Code:
    1. QProcess::ProcessState QProcess::state ()
    To copy to clipboard, switch view to plain text mode 

  3. #3
    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: Is a QProcess thread safe in Qt4?

    Let's answer the title first - QProcess is not thread-safe but it is reentrant.

    Reading variables is usually safe even if the object is not thread-safe so reading the state should work. The proper and completely safe way of what you want to do is to use signals and slots by connecting QProcess::stateChanged() signal to a slot in the thread you need that information in.
    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.


  4. #4
    Join Date
    Aug 2009
    Location
    Boston Area, USA
    Posts
    2
    Qt products
    Qt3 Qt4
    Platforms
    Unix/X11 Windows

    Default Re: Is a QProcess thread safe in Qt4?

    Thanks for the answers. I am going to leave the Qt4 code without the elaborate Qt3 fix that moved all references to the QProcess to the main thread. We have gotten no crashes so far.

    Note that the creation and deletion of the QProcess is already in the main thread due to an earlier fix.

    If I ever do get a crash I will post here, but I don't expect to.

    Totally off topic, but I am unfamiliar with a 'thanks' button. Is it considered good etiquette to push it for all helpful answers? I could see it as a good thing, or a nuisance due to bombardment by thanks nodules depending on how it was implemented.

  5. #5
    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: Is a QProcess thread safe in Qt4?

    You can use it for any post you find useful and would otherwise hit the "Post Reply" button and write "Thanks" in it.
    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. What makes something not thread safe?
    By tgreaves in forum Newbie
    Replies: 9
    Last Post: 20th February 2009, 21:16
  2. GUI and non-GUI thread problem
    By dimaz in forum Qt Programming
    Replies: 3
    Last Post: 18th September 2008, 22:25
  3. KDE/QWT doubt on debian sarge
    By hildebrand in forum KDE Forum
    Replies: 13
    Last Post: 25th April 2007, 07:13
  4. Are QHttp n QHttpRequestHeader thread safe?
    By Shambhavi in forum Qt Programming
    Replies: 4
    Last Post: 21st January 2006, 09:33
  5. QProcess in a QThread
    By chombium in forum Qt Programming
    Replies: 2
    Last Post: 11th January 2006, 16:52

Tags for this Thread

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.