Re: GtkListStore and Threads problems



Here is a graphic to show where (I suppose) the problem is:


Time-->
T0       T1          T2                                T3            T4
                         gtk_main() is blocked
------- |----------|-----------------------|-------------|----->
| | | |
          |               | gtk_list_store_set()     |                    |
| | | | | V V |
          |   gdk_threads_enter()   gdk_threads_leave()    |
| | | | V V
  gtk_tree_model_get()                        gtk_tree_model_get()
  (gtk_main() start)                              (gtk_main()continue)

At time:

T1: gtk_main() starts to read the 5 columns of row 1 from the model shared with
   the working thread (using gtk_tree_model_get())

T2: the working thread locks GTK main thread
   gtk_main() is blocked in the middle of the loop to read the 5 columns

Note: (is this possible??? I'm not sure how gtk_threads_enter() interacts with main thread)

Lets suppose it has read only 2 columns when the working thread locks it.
   Then the working thread upgrades some columns of row 1
   (using gtk_list_store_set() which calls gtk_sequence_* functions), so
   the iter (row 1) changes

T3: working thread releases the lock

T4: gtk_main() continues reading the other 3 columns from the "updated" model (using gtk_tree_model_get()) and it complains because the iter has changed!!!



Regards,
Lorena

----- Original Message ----- From: "Peter Bloomfield" <peterbloomfield bellsouth net>
To: "Lorena Salinas" <lsalinas invap com ar>
Cc: <gtk-app-devel-list gnome org>
Sent: Wednesday, June 15, 2005 1:45 PM
Subject: Re: GtkListStore and Threads problems


On 06/15/2005 10:11:53 AM Wed, Lorena Salinas wrote:
Here is part of the function...

valid = gtk_tree_model_get_iter_first (GTK_TREE_MODEL
(my_model),
&iter);

while (valid) {

gtk_tree_model_get (GTK_TREE_MODEL (my_model), &iter,
  MYMODEL_COL_STATUS, &status,
-1);

/* do some long calculations.... */

gdk_threads_enter(); /* get GTK thread lock */

gtk_list_store_set (GTK_LIST_STORE (my_model), &iter,
MYMODEL_COL_STATUS,STATUS_NORMAL,
-1);

gdk_threads_leave(); /* release GTK thread lock */

valid = gtk_tree_model_iter_next (GTK_TREE_MODEL (my_model),
&iter);

}


This is the only place where I update the model...
Is it valid to assume that iter is the same, that it hasn't
changed?

The iter shouldn't have changed, and if the model isn't changed the iter should still be valid. But your original errors were coming from gtk_sequence_* functions, which are called from gtk_list_store_iter_next() as well as (possibly) gtk_list_store_set(), and you call gtk_list_store_iter_next() without holding the lock.

You could organize the loop around a path instead of an iter, and then when you really need an iter:
- grab the lock;
- initialize the iter;
- use the iter to get or set;
- drop the lock;
and get on with those really long calculations.

Peter






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