当一个线程forks()时,其他线程会怎样? [英] What happens to other threads when one thread forks()?

查看:87
本文介绍了当一个线程forks()时,其他线程会怎样?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在使用pthread的C ++中,如果您的一个线程调用fork,那么其他线程会怎样?

似乎线程不跟随.就我而言,我试图创建一个守护进程,并且使用fork()并退出父进程以使其守护进程.但是,在代码的新路径中,我在派生之前创建了一些线程,在之后创建了一些线程.是否有一种简单的方法可以将线程的所有权更改为新的派生进程,而不是将所有的线程创建都移到派生之后?

解决方案

没什么.只有调用fork()的线程才重复.子进程必须启动任何新线程.父线程不理会.

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?

解决方案

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

这篇关于当一个线程forks()时,其他线程会怎样?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆