What happens to other threads when one thread forks()?

WilliamKF picture WilliamKF · Apr 10, 2012 · Viewed 10.2k times · Source

In C++ using pthreads, what happens to your other threads if one of your threads calls fork?

It appears that the threads do not follow. In my case, I am trying to create a daemon and I use fork() with the parent exiting to deamonize it. However, in a new path through the code, I create some threads before the fork and some after. Is there an easy way to change ownership of the threads over to the new forked process rather than moving all my thread creation after the fork?

Answer

Andrew T Finnell picture Andrew T Finnell · Apr 10, 2012

Nothing. Only the thread calling fork() gets duplicate. The child process has to start any new threads. The parents threads are left alone.