为什么在Erlang中需要spawn_link? [英] Why is spawn_link necessary in Erlang?

查看:269
本文介绍了为什么在Erlang中需要spawn_link?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我了解到,如果您在链接后生成链接,则该过程可能在此期间已终止。为什么会有问题呢?不能链接看到您正在尝试链接到已经终止的进程吗?在那种情况下,它的行为就好像远程进程在调用链接后立即消失一样吗?

I understand that if you do spawn followed by link, the process may have died in the mean time. Why is that a problem? Can't link see that you're trying to link to a process that has already died? In that case couldn't it just behave as though the remote process died immediately after link was called?

我认为如果可以单独生成和链接会很好,而不必在一个原子函数中一起完成操作,因为a)会使语言更加正交(spawn_link与spawn和link严重重叠)b)如果我有一个启动函数,基本上只是将spawn包裹起来,我也需要提供start_link。因此,非正交性是病毒式的。

I think it would be nice if you could do spawn and link separately, and not have to do them together in one atomic function, because a) that would make the language more orthogonal (spawn_link heavily overlaps with spawn and link) b) if I have a start function that's basically just a wrapper around spawn, I ALSO need to supply start_link. So the non-orthogonality is viral. Yuck!

推荐答案

请记住链接是双向的,因此请考虑以下情况:进程A生成进程B,然后在能够死亡之前死掉链接到B。在这种情况下,B不知道它没有链接到A,并且在A死亡时也不会死亡。

Remember that links are bidirectional, so consider the case where process A spawns process B but then dies before being able to link to B. In this case, B has no idea that it is not linked to A, and it does not die when A dies.

With spawn_link 这种情况不可能发生,因为产生和链接要么原子地发生在一起,要么它们都失败了。

With spawn_link this scenario can't happen because the spawn and link either occur together atomically, or they both fail.

这篇关于为什么在Erlang中需要spawn_link?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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