PDA

View Full Version : Two QProcess object access to same function



pronetin
15th February 2011, 08:28
Hi all,

I have two QProcess objects. They run two different programs and their finished signals were handled.
p1_handlefinished function is for first object and p2_handlefinished function is for second object.

In p1_handlefinished and p2_handlefinished functions, i call a function myfunc;

Should a lock(mutex) control accessing to myfunc or it doesn't need?:confused:

I use QT4 on Linux.

high_flyer
16th February 2011, 10:49
it depends on what myfunc() is doing.

pronetin
19th February 2011, 05:18
myfunc() has this prototype:
void myfunc(QString str)

This function appends to TextBrowser given str.

I think that i should control calling myfunc by a lock, because only one instance of myfunc() exists and two QProcess objects should use it.
Is it right?

Please guide me completely or give me some references so that i read them myself.

Thanks you very much.

high_flyer
20th February 2011, 11:44
In general if you have a resource that can be accessed asynchronously at the same time from different threads, then you need to protect that resource with some mutual exclusion mechanism.
So its not the function that needs to be mutexed, but the resource that is being accessed.
In your case, actually, I think you don't need any mutexes, since your code lives in the main GUI thread.
So your application is not threaded.
The QProcess signals will not be called in parallel (even though each QProcess runs its own thread), since they are being handled by the QApplication event loop.