is the GSList library MT safe ?

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)

/* compile with > gcc gslist.c `gtk-config --cflags --libs` -lpthread */

#include <stdio.h>
#include <errno.h>
#include <pthread.h>
#include <glib.h>

void *myLoop(void *data) {
  GSList *list=NULL;
  int i,n=30;

  for (i=0;i<n;i++) list = g_slist_append(list,(gpointer *)i);

  while (1) {
    list = g_slist_append(list,list->data);
    list = g_slist_remove(list,list->data);
  return NULL;

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) 

  while (1) {
    list = g_slist_append(list,list->data);
    list = g_slist_remove(list,list->data);
  return 0;

And here is what gdb has to say:

>gdb gslist
GNU gdb 4.18
Copyright 1998 Free Software Foundation, Inc.
GDB is free software, covered by the GNU General Public License, and you are
welcome to change it and/or distribute copies of it under certain conditions.
Type "show copying" to see the conditions.
There is absolutely no warranty for GDB.  Type "show warranty" for details.
This GDB was configured as "i386-redhat-linux"...
(gdb) run
Starting program: /home/rougeaux/src/test/gslist/gslist 
[New Thread 3646 (manager thread)]
[New Thread 3645 (initial thread)]
[New Thread 3647]
[Switching to Thread 3647]

Program received signal SIGSEGV, Segmentation fault.
0x401868b9 in g_slist_alloc () from /usr/lib/
(gdb) info stack
#0  0x401868b9 in g_slist_alloc () from /usr/lib/
#1  0x40186aba in g_slist_append () from /usr/lib/
#2  0x80487bf in myLoop (data=0x0) at gslist.c:13
#3  0x40264eca in pthread_start_thread (arg=0xbf7ffe60) at manager.c:213

Any clue ?

Sebastien Rougeaux
RSISE, The Austrlian National University

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