threads in GTK+ HELP!



Hi,

I've made a program using GTK+ 2.0
Program has GUI, and it calculates some mathematic equations in backgroud and 
after that, it draws a graph in gtk drawable, using GDK functions (it sets 
points in some pixels on pixmap, and on expose_event it copy pixmap to 
gtk_drawable) 
I can also load calculates data from file, and draw a graph.
There are few buttons on GUI to customize calculations and a graph drawing.
Everything seems works fine until I use a threads. I want to use them, because 
mathematical calculation takes about 15 minutes, when GUI is not active, and 
I can't use more than one CPU.
There is one function in my program (procedure_start - it calculates 
everything), that I wanted to go on separate thread (just for the beginning). 
This function is called when I press button on GUI which calls other function 
in callbacks.c file.

So, I've made:
        g_thread_init(NULL)
in main() before any other function like: gtk_init()
and in function: on_start_button_clicked (from callbacks.c file)
I've made:
        gdk_threads_init();
        gdk_threads_enter();
        iret1=pthread_create(&thread1,NULL,procedure_start,(void*)window1);
        gdk_threads_leave();
I've also tried:
        GThread *procedure=NULL;
        procedure=g_thread_create(procedure_start,(gpointer)window1,FALSE,NULL);
with gdk_threads_init() and gdk_threads_enter() in main.c just after 
g_thread_init()

My program crashes randomly after I start procedure in separate thread, when 
I'm not starting procedure I can do everything else (draw graph from data in 
files, switch some buttons on GUI, etc.)
But when I start "procedure_start" in separate thread and then I try use GUI 
it crash. It crash randomly(when I press some buttons on GUI) and always when 
I try to draw a graph from file.
When I debug it there was:
stack (read from bottom)
in mallopt() from /lib/tls/libc.so.6
in malloc() from /usr/tls/libc.so.6
in g_malloc() from /usr/lib/libglib-2.0.so.0
in draw_graph_from_file (data=0x81de828) at graph.c:41
in ....
in gtk_main () from /usr/lib/libgtk-x11-2.0.so.0

on debug screen (I use Anjuta)
[Thread debugging using libthread_db enabled]
[New Thread -1216529920 (LWP 9633)]
[Switching to Thread -1216529920 (LWP 9633)]
main (argc=1, argv=0xbffff954) at main.c:20   //this is where g_thread_init() 
[New Thread -1218065488 (LWP 9666)]  //this is where procedure_start

Program received signal SEGSEGV, Segmentation fault. //this probably when I 
choose file with data from gtkfilechooser to draw a graph

0xb7a41db6 in mallopt() from /lib/tls/libc.so.6

In code this crash was when I tried to allocate some memory for reading data 
from file:
buffer=(char *)g_malloc(200000*sizeof(char));

I've to say that I'm very beginning in GTK and in C programming to.
In my program I don't use any mutex(g_mutex) or GCond and others, but when 
procedure_start runs in one thread nothing else is using this same variables, 
besides race conditions can crash application? It is rather problem with 
memory allocation (there are sometimest crashes when program calls free() or 
g_free() )
Can I allocate memory in some threads? - I don't know, but I think I can.???

In:  http://developer.gnome.org/doc/API/2.0/gdk/gdk-Threads.html
there is written about gdk_threads_enter():
"This macro marks the beginning of a critical section in which GDK and GTK+ 
functions can be called. Only one thread at a time can be in such a critial 
section"
But on this same page there is an example from Erik Mouw, that creates two 
threads in section between gdk_threads_enter() and gdk_threads_leave()
So, what does it mean "critical section" ?

I don't expect someone solve my problem, but maybe for a guy with experience 
it can be quite simple what I'm doing wrong?
Where should I look? Where to pay attention?
Why program works fine in one thread (no SIGSEGV with memory allocation) and 
crash randomly with two threads?

I can send some more code or debug if needed.
I'm using debian SID.

Thanks for any help, and sorry for bad english.
Marek



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