error with thread



Hi,

I have a so big function (near 600 lines) and it communicates with
some embedded system using serial port... this function have some ties
(for and while).. and a great number of usleep...

When I call this function, GTK windows and widgets are lost.. and this
mustn't be happen... to resolve this I'm using threads like this
example:

/* 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 );
}

//end

I'm using exactly like this example, but all stopped widgets keep
going like before.. have this function any limitations about ties
number (for, while) or time under the while(1) of the thread?

Thanks!

-- 
Abraço,
Frederico Schardong,
SOLIS - O lado livre da tecnologia
www.solis.coop.br


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