[Glade-users] uni- versus multi-threaded gtk



On Sat, 2011-06-25 at 11:01 -0700, David Buchan wrote:
// main calculation loop
while (flag == 0) {
  make some heavy duty calculations;

// Check if the user has clicked any buttons (e.g., the STOP button)
    while (gtk_events_pending()) {
      gtk_main_iteration();      // let the main gtk loop deal with it
    }
  carry on with more ridiculously heavy duty calculations;
}

The way I set it up, if the user clicks the STOP button, a callback is
executed which sets the flag to zero (flag = 0). The main calculation
while-loop then ends peacefully.

It's not the preferred way to do it. The preferred way is to replace
your loop with an idle function which is called repeatedly whenever
there's no other events are to be processed. You use g_idle_add() to add
the function to the main loop and the main loop takes care of all the
necessary checks. All you have to do in the function is returning TRUE
until the job is done and finally return FALSE to stop the function
being called. To cancel the job by the user request you'd use
g_source_remove() in the appropriate callback.

The approach you described is acceptable only for porting some old code
relying on polling user input within some complex loop which is too
burdensome to untangle. In general, unless you do some low level stuff,
if you feel the need to poll for events you probably doing something
wrong, cause it is the main loop job to decide when and what to process.





[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]