Beefy Boxes and Bandwidth Generously Provided by pair Networks
go ahead... be a heretic

Re^3: Gtk2 app -- what's better, threads, or multiple timeouts?

by traveler (Parson)
on Feb 06, 2009 at 16:40 UTC ( #741950=note: print w/replies, xml ) Need Help??

in reply to Re^2: Gtk2 app -- what's better, threads, or multiple timeouts?
in thread Gtk2 app -- what's better, threads, or multiple timeouts?

If you are experiencing lag, then either 1) run the main loop occasionally during cpu-intensive functions

Okay I'm not sure I understand this. I was under the impression that the main loop was always running. Does this mean that when I run a timeout, the main loop "waits" for whatever the timeout does to finish before it continues on? Observing my program it does seem like that is the case.

The main loop waits for events. When it gets one it processes it. If the event it is processing is a timer event tied to a sub of yours, it runs the sub. (Don't) try this

while (1) { ... }
It will hang the GUI. So in an event-driven environment such as Gtk2 you allow events to trigger subs. Never have your code sit around waiting for something to happen, that is the main loop's job. When something happens it will trigger an event (if you ask it to) and run your sub. The only time you should ask Gtk2 to process pending events if if you have a compute intensive block of code. For instance, if you are sorting a few zillion items, every once in a while (pun intended) try
Gtk2->main_iteration while Gtk2->events_pending;
This runs the main iteration code (which is what loops in the main loop while there are any events pending. Of course, you need to make sure that this does not mangle the data you are processing.

Log In?

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://741950]
and John Coltrane plays...

How do I use this? | Other CB clients
Other Users?
Others pondering the Monastery: (2)
As of 2017-07-22 21:28 GMT
Find Nodes?
    Voting Booth?
    I came, I saw, I ...

    Results (341 votes). Check out past polls.