Re: is popen thread safe?



LiNuCe,

I have tried the the g_spawn_command_line_sync, the simple version of
g_spawn_sync, but it work as the popen.

I have attached a example of what I'm trying to do. As you can see, in
this example I have two threads, and in the thread_func I have the
g_spawn_command_line_sync that call a soap php script that get
informations of a user on my intranet web server. The problem is that
when the g_spawn_command_line_sync is called the multi thread system
is broken, the other thread is stopped and waits for this thread ends!
This is the same problem with the popen, the others threads are
stopped and waits for the soap thread end.

Someone can help me? I don't know why this is happen...

2010/5/8 LiNuCe <linuce gmail com>:
>> I'm trying to add the popen in a thread (using g_thread_create()) but
>> it keep frozen my application. So, is popen thread safe? there is
>> another simple alternative if it isn't?
>
> Try GLib's simple, portable alternative :
>
>  http://library.gnome.org/devel/glib/stable/glib-Spawning-Processes.html#g-spawn-sync
>



-- 
Thanks,
Frederico Schardong,
SOLIS - Open source solutions
www.solis.coop.br
Linux registered user #500582
/* 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();
        gchar *my_stderr = NULL;
        gchar *my_stdout = NULL;
        GError *my_error = NULL;
        gint my_return_value;


        if( !g_spawn_command_line_sync("php5 /home/frederico/Desktop/xponto/xponto_C/classes/soap/soap.php loginf user passord", &my_stdout, NULL, NULL, NULL))
        {
            printf("I could not run the command\n");
        }
        else
        {
            // command worked
            printf("STDOUT:\n%s\n\n", my_stdout);
        }
        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 );
}


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