Re: how to leave a specific thread?



the question should be changed to: how do i tell a thread to kill itself?

glib does not allow for killing a thread from another thread.  this is a bad idea since killing a thread from another does not allow for the thread being killed to shutdown any processing it is doing in a graceful manner.  thus possibly leaving the program in a crap state.

what needs to happen is that the thread that wants to do the killing has to provide some sort of communication mechanism that is then picked up by the thread to be killed.  the thread to die then shuts itself down in a graceful manner.

which mechanism is a matter of choice on the part of the programmer.  a global variable, for example, or, better yet, usage of GAsyncQueue.

richard


On Sat, Jul 11, 2009 at 11:30 PM, Sergei Steshenko <sergstesh yahoo com> wrote:



--- On Sat, 7/11/09, frederico schardong <frede sch gmail com> wrote:

> From: frederico schardong <frede sch gmail com>
> Subject: how to leave a specific thread?
> To: gtk-list gnome org
> Date: Saturday, July 11, 2009, 12:08 PM
> I'm using this example of threads..
>
> /* Compile me with:
>  *  gcc -o sample3 sample3.c $(pkg-config --cflags
> --libs gtk+-2.0 gthread-2.0)
>  */
> #include <gtk/gtk.h>
>
> static gpointer
> thread_func( gpointer data )
> {
>     while( TRUE )
>     {
>         usleep( 500000 );
>
>         gdk_threads_enter();
>         g_print("\naa");
>         gdk_threads_leave();
>     }
>
>     return( NULL );
> }
>
> static gpointer
> thread_func1( gpointer data )
> {
>     while( TRUE )
>     {
>         sleep( 1 );
>
>         gdk_threads_enter();
>         g_print("\nbb");
>         gdk_threads_leave();
>     }
>
>     return( NULL );
> }
>
> int
> main( int    argc,
>       char **argv )
> {
>     GThread   *thread, *th;
>     GError    *error = NULL;
>
>     /* Secure glib */
>     if( ! g_thread_supported() )
>         g_thread_init( NULL );
>
>     /* Secure gtk */
>     gdk_threads_init();
>
>     /* Obtain gtk's global lock */
>     gdk_threads_enter();
>
>     /* Do stuff as usual */
>     gtk_init( &argc, &argv );
>
>     /* Create new thread */
>     thread = g_thread_create( thread_func, NULL,
>                
>               FALSE,
> &error );
>     if( ! thread )
>     {
>         g_print( "Error: %s\n",
> error->message );
>         return( -1 );
>     }
>
>     /* Create new thread */
>     th = g_thread_create( thread_func1, NULL,
>                
>               FALSE,
> &error );
>     if( ! th )
>     {
>         g_print( "Error: %s\n",
> error->message );
>         return( -1 );
>     }
>
>     gtk_main();
>
>     /* Release gtk's global lock */
>     gdk_threads_leave();
>
>     return( 0 );
> }
>
>
> And I must know how I can stop some thread without stop all
> the
> others? Just stop one thread, not all
>
> --
> Abraço,
> Frederico Schardong,
> SOLIS - O lado livre da tecnologia
> www.solis.coop.br


You can still use global variables, and threads can monitor their value.

Kinda signals.

Or I'm getting it wrong ?

Regards,
 Sergei.



_______________________________________________
gtk-list mailing list
gtk-list gnome org
http://mail.gnome.org/mailman/listinfo/gtk-list



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