Re: gtk thread begginer



On Thu, 2005-02-17 at 00:05, Boris Winter wrote:
do somebody can sand an simple example how to use gtk threads?
how can i application like counter?

I don't completly understand what you say here, and what's the "counter"
application you want to implement.

I you'd narrow a bit your question, may be we'd be able to tell you if
threading is the best approach for your needs.

You can find information on threads here:
http://developer.gnome.org/doc/API/2.0/glib/glib-Threads.html

As a matter of example a simple application that spawns a couple of
threads could be:
///////////////////////////////////////////////////////////////////////
// copy and paste to a file, and compile with: 
// gcc file.c `pkg-config --cflags --libs gtk+-2.0 gthread-2.0` -o file
#include <gtk/gtk.h>

typedef struct _ThreadData {
        gchar* name; // somthing to diferentiate both threads
        gint sleep;  // sleep time on each iteration
} ThreadData;

// each thread will iterate 10 times writting to
// stdout, and sleep the given time on each iteration
gpointer
thread_func (gpointer user_data)
{
        ThreadData *data;
        gint i;

        data = (ThreadData*) user_data;
        for(i=0;i<10;i++) {
                g_print("Thread running %s - I advanced %d\n", data->name, i);
                sleep(data->sleep);
        }
        
        g_free(data->name);
        g_free(data);
        return 0;
}
int
main (int argc, char **argv)
{
        GThread *thread;
        GThread *thread2;       
        GError *error = NULL;
        ThreadData *data;

        // is the threading subsystem activated ?
        if (!g_thread_supported ())
        {
                g_thread_init (NULL);
        }

        // data for the first thread
        // will be disposed by the thread itself
        data = g_malloc(sizeof(ThreadData));
        data->name = g_strdup("Jhon");
        data->sleep = 1;

        // create and launch the thread
        thread = g_thread_create (thread_func, // funtion
                                         data, // data for hte thread
                                         TRUE, // joinable ?
                                         &error); // on error store here

        // sanity check, thread will be NULL
        // if something failed
        if( !thread )
        {
                g_print("Error %s", error->message); 
                return -1;
        }   

        // The same for the second thread
        data = g_malloc(sizeof(ThreadData));
        data->name = g_strdup("Paul");
        data->sleep = 2;

        thread2 = g_thread_create (thread_func,
                                         data, 
                                         TRUE,
                                         &error);

        if( thread==NULL )
        {
                g_print("Error %s", error->message);  
                return -1;
        } 

        // wait for them to finish
        // joinable must be TRUE
        // on g_thread_Create()
        g_thread_join (thread);
        g_thread_join (thread2);
        
        return 0;
}
/////////////////////////////////////////////////////////

Regards.
-- 
Iago Rubio



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