[clug] long run function in trolltech qt widget

Daniel Pittman daniel at rimspace.net
Thu Dec 4 22:07:14 GMT 2008


Brad Hards <bradh at frogmouth.net> writes:
> On Thursday 04 December 2008 09:50:11 am jm wrote:
>
>> doWork() doesn't return until it's actually finished the calculation.
>> There isn't a contiuation unfortunately. How do I call doWork() so that
>> MyPage is displayed while the work is being done? I've been trying to
>> avoid a thread for this as it only meant to be a prototype at this stage
>> and QThread looks as if it will take a while to understand. Especially
>> getting values in and out of threads.
>
> Well. if you don't return to the event loop, it isn't going to update the GUI.
>
> You could invoke a process (as Crash suggested) and read the results
> off stdout with QProcess, connecting the readyReadStandardOutput
> signal to a slot that updates the QProgressBar.

This is a reasonable choice.  You could also use fork(2)[1] rather than
writing an entirely new process, and/or use a socket or pipe pair for
communication.

> You could use a QThread (its not hard, but make sure you read the
> docs).

Hah.  This, to me, seems like a rather risky statement: the original
poster stated that this was a wrapped C library, which starts us off on
an exciting foot.

Are you certain that the library doesn't make unsafe use of a signal
handler?  How about those non-entrant libc functions?  Do /you/ want to
explain appropriate locking to protect shared data structures?

Threads /look/ easy, but they are actually really hard.  Because you are
sharing the memory, file descriptor, signal and working directory
namespaces between threads /any/ problem is a global problem.

Sticking with the additional protection of a distinct process, through
fork(2) or fork(2) and execve(2), is generally much easier than threads.


(And, yes, threads often /seem/ to work, until you discover that your
 software randomly crashes on some subset of machines in production, in
 ways that are effectively impossible to debug.  Not the path to
 reliable software, and also often lower performance as a bonus.)

Regards,
        Daniel

Footnotes: 
[1]  IIRC, Qt has some wrapping of fork that makes it easier to use with
     your process, but I can't recall where.



More information about the linux mailing list