Re: is popen thread safe?



On Fri, 14 May 2010 15:31:23 -0300
frederico schardong <frede sch gmail com> wrote:

> 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...

The first thing to say is that your use of the GDK global lock is
pointless in this particular example, and invites problems.

In addition, in a multithreaded program you can only call
async-signal-safe functions between a fork() and an exec(), and you
have no guarantee that popen() and/or the glib spawning functions obey
this requirement (they may or may not, but since both popen() and
g_spawn_command_line_sync() call up a shell, it is highly unlikely that
they do). It is particularly dangerous if you spawn when holding a lock.

If in doubt, code it by hand using exec*() (and note that few functions
are async-signal-safe: for those that are, see POSIX 1003.1-2001, System
Intefaces, Signal Concepts, Section 2.4.3), but you will probably be
if you use the glib spawning functions which don't call up a shell.

Chris




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