Re: is the GSList library MT safe ?




Sebastien Rougeaux <Sebastien.Rougeaux@syseng.anu.edu.au> writes:

> I am wondering if the GSList utility provided by glib is really
> Multi-Thread safe. Here is a little example that do simple list
> operations in two separate threads (for 2 separate lists), and
> that crashes all the time for me (using redhat 6.1, gtk+-1.2.5)

[...]

> int main(int argc, char *argv[]) {
>   GSList *list=NULL;
>   int i,n=30;
>   pthread_t t_id;
> 
>   for (i=0;i<n;i++) list = g_slist_append(list,(gpointer *)i);
> 
>   if (pthread_create(&t_id, NULL, myLoop, (void *)NULL) == -1) 
>     perror("pthread_create");
> 
>   while (1) {
>     list = g_slist_append(list,list->data);
>     list = g_slist_remove(list,list->data);
>   }
>   return 0;
> }

Compile with glib-config --libs gthread and add a 
call g_thread_init(NULL) at the beginning of
main(), and things should work better.

Regards,
                                        Owen



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