Re: using pthreads with fork() in linux




Arnold Hendriks wrote:
"Jelte" <jelte.werkhoven@xxxxxxxxxxxxxxx> wrote in message
news:1148974504.450517.232000@xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx

Elena wrote:
Hi, I have the following situation:
in the main() I create a thread by using pthread_create(), then my
main() forks,
the child program performs some action and exits. The parent waits for
it with "waitpid()".
For one, you should only use async-signal-safe functions after fork(), until
you exec().

This is wrong. You are probably talking about vfork().

If you happen to fork while another thread still holds an
important lock, that thread wouldn't exist in the forked copy and the lock
would never be released inside the child, causing a deadlock

This is why there is pthread_atfork.
http://www.opengroup.org/onlinepubs/009695399/functions/pthread_atfork.html

.