Thread problem



hi,

I have this thread:

pthread_create( &add_to_list_tid, NULL, (void *)add_to_clist, (GtkCList *)list);

It works (the list entries pop up one after another) when the thread function looks like this:

void *add_to_clist( void * arg)
{
        GtkCList * data = (GtkCList *)arg;
        gchar *entries[5] = { "bbbbb", "prttpritpr", "busybusy", "bee", "gone" };
        int count = 0;

        for( count = 0; count < 5; count++)
        {
                gdk_threads_enter();
                gtk_clist_append(( GtkCList *)data, &entries[count]);
                gdk_threads_leave();
                sleep(1);
        }
}


But when I replace the function with the following, all the list entries show at the same time when the function is finished.
Why is that?


void *add_to_clist( void * arg)
{
        GString *output;
        gchar **list_entry;
        char buffer[11];
        int data_processed;
        int file_pipes[2];
        pid_t fork_result;
        char *entry_end;
        GtkCList * data = (GtkCList *)arg;

        if( pipe( file_pipes) == 0)
        {
                fork_result = fork();
                if( fork_result == ( pid_t) -1)
                {
                        fprintf( stderr, "Fork failure");
                        exit( EXIT_FAILURE);
                }
                if( fork_result == ( pid_t) 0)
                {
                        close(1);
                        dup( file_pipes[1]);
                        close( file_pipes[0]);
                        close( file_pipes[1]);
                        execlp( "./test.pl", "./test.pl", "./test.pl", (char *)0);
                        exit( EXIT_FAILURE);
                }
                else
                {
                        close( file_pipes[1]);
                        data_processed = read(file_pipes[0], buffer, 10);
                        output = g_string_sized_new( 50);
                        while( data_processed > 0)
                        {
                                buffer[data_processed] = '\0';
                                output = g_string_append( output, buffer);
                                entry_end = (char *)strchr( output->str, '\n');
                                while( entry_end != NULL)
                                {
                                        *entry_end = '\0';
                                        list_entry = g_strsplit( output->str, ",", 0);
                                        gdk_threads_enter();
                                        if( list_entry[1] != NULL)
                                        {
                                                gtk_clist_append(( GtkCList *)data, list_entry);
                                        }
                                        else
                                        {
                                                gtk_clist_set_column_title((GtkCList *)data, 0, 
list_entry[0]);
                                                gtk_clist_set_column_title((GtkCList *)data, 1, 
list_entry[0]);
                                                gtk_clist_column_titles_show((GtkCList *)data);
                                        }
                                        gdk_threads_leave();
                                        g_strfreev( list_entry);
                                        output = g_string_assign( output, entry_end + 1);
                                        entry_end = (char *)strchr( output->str, '\n');
                                }
                                data_processed = read(file_pipes[0], buffer, 10);
                        }
                        close( file_pipes[0]);
                }
        }
}
_________________________________________________________________________
Get Your Private, Free E-mail from MSN Hotmail at http://www.hotmail.com.

Share information about yourself, create your own public profile at http://profiles.msn.com.





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