Re: Spawn a process without exec



On Fri, 20 Aug 2004 00:22:33 +0000, David Rosal <davidrr menta net> wrote:
John Cuppit wrote:
sounds like you want to start a child thread:

  http://developer.gnome.org/doc/API/2.0/glib/glib-Threads.html

Uhrgh! I dreaded something like that. It's (always) time to learn...

It's really pretty easy, very similar to fork(), except that the new
thread runs in the same address space, and it's a function that gets
called rather than one side of an if() that gets taken. The only two
API calls you need are thread create and thread join. Plus a little
commmunication and synchronisation stuff.

there are some 'gotchas' if you have several threads all calling the
same GTK. It's usually best (IMO) to have all of the GTK calls in one
thread and to use something like an asyncqueue for workers to tell the
GUI thread what to do.

At the moment I know almost nothing about glib threads, but after
taking a first look at the API reference I couldn't find any way to
"kill" a thread from the main thread, which is what I want to do.
There's g_thread_exit(), but it exits the current thread, and
g_thread_join() waits for the thread to exit by itself...

Apparently thread cancel is very hard to implement portably. Instead I
usually have a flag somewhere which the main thread sets when it wants
the child to exit.

The good thing about doing it this way is that the child then gets a
chance to clean up. Because the child thread runs in the same address
space, there's no automatic freeing of memory / closing of files /
etc. when the child dies. Thread cancel would (probably) cause
terrible resource leaks.

John



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