避免竞争条件
以下代码片段摘自 Francesco Cesarini 和 Simon Thompson 所著《Erlang 编程》一书的第 112 页,作为 Erlang 中可能存在的竞争条件的说明。
start() ->
case whereis(db_server) of
undefined ->
Pid = spawn(db_server, init, []),
register(db_server, Pid),
{ok, Pid};
Pid when is_pid(Pid) ->
{error, already_started}
end.
在不逐字复制详细信息的情况下,作者解释说,如果两个进程同时执行 start(),则运行“未定义”部分的进程 1 可能无法完成,因为进程 2 会导致它被抢占。然后,进程 2 将运行“未定义”部分直至完成。现在,当进程 1 恢复时,db_server 已被进程 2 注册,导致其对 register() 的调用引发运行时错误。我希望你能明白我的意思,因为我不想窃取这本书的文字。
我的问题是如何对上述精确功能进行编码以避免两个进程同时执行 start() 时潜在的竞争条件?
The following code snippet is taken page 112 of the book by Francesco Cesarini and Simon Thompson, Erlang Programming, as an illustration of a possible race condition in Erlang.
start() ->
case whereis(db_server) of
undefined ->
Pid = spawn(db_server, init, []),
register(db_server, Pid),
{ok, Pid};
Pid when is_pid(Pid) ->
{error, already_started}
end.
Without copying the details verbatim, the authors explain that if two processes simultaneously execute start(), then process 1 running the "undefined" section might not complete, because process 2 causes it to be preempted. Process 2 would then run the "undefined" section to completion. Now, when process 1 resumes, db_server has already been registered by process 2, causing the its call to register() to throw a runtime error. I hope you can understand what I mean, because I don't want to filch the book's text.
My question is how can the above exact functionality be coded to avoid the potential race condition when two processes simultaneously execute start()?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
通常,这是通过让生成的进程注册自己的名称,然后向其父进程发回响应,告诉父进程是否成功来解决的。
(可能无法编译或工作。在此处键入而不进行检查。:))
您可以在 gen.erl 中找到该版本的精心实现的版本。实际上,在实际代码中,您只需使用 OTP 行为来重用该版本。
Generally this is solved by having the spawned process register its own name, and then send back a response to its parent telling the parent whether or not it succeeded.
(May not compile or work. Typed in here without checking. :))
You can find a carefully implemented version of this in gen.erl. In practice, in real code you just use the OTP behaviours to reuse that version.
您可以使用 gen_server 来序列化请求。
You might use a gen_server to serialize the requests.
您要启动多少台服务器?您最初的问题暗示了一个,而对 @cthulahoops 的评论则暗示了两个,一台服务器和一台备份。对于两台服务器,您可以尝试类似的操作:
不过我还没有运行它。
How many servers do you want to start? Your original question implies one, while a comment to @cthulahoops says two, a server and a backup. For two servers you could try something like:
I haven't run it though.