Re: trouble with terminating pthreads in GTK



Hi Shao,

> I am writing a GTK app which plots some graph from realtime data.
> 
> When the start button is pressed in this app, it creats two threads, one
> is for reading the input and one is for drawing the graph.
> 
> pthread_create(&input_pid, NULL, read_input, NULL);
> pthread_create(&draw_pid, NULL, draw_thread, NULL);
> 
> The drawing thread looks something similar to this:
> 
> void *draw_thread(void *arg)
> {
>     pthread_setcanceltype(PTHREAD_CANCEL_ASYNCHRONOUS, NULL);
>     while (terminate_cond==FALSE) {
>         /* some code here to wait for a drawing signal
>          * to update the graph
>          */
>          pthread_mutex_lock(blah...)
>          pthread_cond_wait(blah...)
>          pthread_mutex_unlock(blah...)
> 
>          /* !!! Sometimes, this thread failed to terminate
>           * and hangs here
>           */
>          gdk_threads_enter();
>          draw();    /* all the drawing code go in this function */
>          gdk_threads_leave();
>     }
> }
> 
> However, I am having some trouble to terminate this drawing thread when
> the stop button is pressed. I use this to terminate the drawing thread;
>     pthread_cancel(draw_thread);
> 
> I guess this is a deadlock situation between the drawing thread and the
> GTK's main thread, but I don't know how to resolve this. Your help would
> be much appreciated.

The solution is quite simple. Don't use thread cancelation (which is a bogus
thing anyway). Just do it with 'terminate_cond' as you already do and make
draw() either looking at terminate_cond as well or only running very short
periods of time.

Bye,
Sebastian
-- 
Sebastian Wilhelmi                   |            här ovanför alla molnen
mailto:wilhelmi ira uka de           |     är himmlen så förunderligt blå
http://goethe.ira.uka.de/~wilhelmi   |




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