Erlang.关于?SERVER和?MODULE宏的区别的问题

voase2hg  于 2022-12-08  发布在  Erlang
关注(0)|答案(1)|浏览(152)

gen_server 实现的所有示例中,我看到 ?SERVER 被分配给 ?MODULE

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

我的想法是运行许多不同名称的服务器进程,但在一个模块中实现。但是,当我在实验中试图运行与模块名称不同的服务器时,我总是出错。有人能给我解释一下这个微妙之处吗?

vsikbqxv

vsikbqxv1#

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. 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.

相关问题