简体   繁体   English

二郎。 关于 ?SERVER 和 ? 的区别的问题模块宏

[英]Erlang. Question about the difference of ?SERVER and ? MODULE macros

In all samples of gen_server implementations I've saw the ?SERVER is assigned to ?MODULE .gen_server实现的所有示例中,我看到?SERVER被分配给?MODULE Look down here:往下看:

-define(SERVER, ?MODULE).
...    
gen_server:start_link({local, ?SERVER}, ?MODULE, [], [])

The idea, I have clued is to run many server processes with different names but implemented in one module.我的想法是运行许多具有不同名称但在一个模块中实现的服务器进程。 But, when I tried to run server with the name different from module name in my experiments, I always got errors.但是,当我在实验中尝试使用与模块名称不同的名称运行服务器时,我总是遇到错误。 Can, please, somebody explain me this subtlety.请有人向我解释这种微妙之处。

The code you show does not and cannot implement multiple servers with different names, since the server name is defined as the same as the module name.您显示的代码没有也不能实现具有不同名称的多个服务器,因为服务器名称定义为与模块名称相同。 So if you try with this code to get multiple servers implemented in one module your attempts will fail.因此,如果您尝试使用此代码在一个模块中实现多个服务器,您的尝试将失败。

The reason for introducing separate SERVER macro with the same value as MODULE is to make things more explicit.引入与 MODULE 具有相同值的单独 SERVER 宏的原因是为了使事情更加明确。 In start_link call the two macros may have the same value, but they serve different purposes, so it is clearer to use two instead of one.在 start_link 调用中,两个宏可能具有相同的值,但它们的用途不同,因此使用两个而不是一个更清晰。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM