PDA

View Full Version : Is a QProcess thread safe in Qt4?



Jay_D
25th August 2009, 21:50
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.

yogeshgokul
26th August 2009, 06:17
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.

QProcess::ProcessState QProcess::state ()

wysota
26th August 2009, 08:17
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.

Jay_D
1st September 2009, 16:27
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.

wysota
1st September 2009, 16:38
You can use it for any post you find useful and would otherwise hit the "Post Reply" button and write "Thanks" in it.