[clug] long run function in trolltech qt widget
Daniel Pittman
daniel at rimspace.net
Fri Dec 5 03:18:35 GMT 2008
jm <jeffm at ghostgun.com> writes:
> Daniel Pittman wrote:
>>
>>> 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?
>
> Luckily this assumption holds.
Now, see, this is one of the most problematic aspects of threading: are
you *sure* it really works, or is it just that there is a 1 in 1000
chance that it will crash?
Are you sure that it doesn't receive a corrupt response (or assertion
failure) from libc when it happens to just sneak in under the wire?
Worse, are you *sure* this will be true tomorrow, when you add a cool
new feature to your wizard, or when the library is enhanced?
> I got the thread version going yesterday, but the problem now is that
> it runs multiple times. The thread start method it called from within
> the wizard's page showevent() method, but it seems that in the course
> of things it receives multiple QEvent::Show events. I've tried various
> way of having it run only once, but seem to be missing something.
You mentioned that you found a solution but traditionally:
// please forgive syntax, as I have not written Qt/C++ for a year or so,
// so am probably a bit rusty...
class Example {
public slots:
void handle_show_event(void);
private:
bool started_thread; // initialize to false in the constructor
};
void Example::handle_show_event(void) {
if (started_thread) return;
started_thread = true; // do this first to avoid a race...
QThread::start();
}
Regards,
Daniel
More information about the linux
mailing list