erlanggen-server

gen_server: what is `Mon` in `start_monitor/4`?


The OTP docs say the signature for gen_server:start_monitor/4 is:


start_monitor(ServerName, Module, Args, Options) -> Result
OTP 23.0
Types
ServerName = {local,Name} | {global,GlobalName}
  | {via,Module,ViaName}
 Name = atom()
 GlobalName = ViaName = term()
Module = atom()
Args = term()
Options = [Option]
 Option = {debug,Dbgs} | {timeout,Time} | {hibernate_after,HibernateAfterTimeout} | {spawn_opt,SOpts}
  Dbgs = [Dbg]
   Dbg = trace | log | statistics | {log_to_file,FileName} | {install,{Func,FuncState}}
  SOpts = [term()]
Result = {ok,{Pid,Mon}} | ignore | {error,Error}
 Pid = pid()
 Error = {already_started,Pid} | term()

What is Mon? The docs just say:

"Mon is a reference to the monitor set up to monitor the server"

So now I know Mon is a monitor and that it monitors, but what is its type?

Reading erlang.erl, I found:

-type registered_name() :: atom().
-type registered_process_identifier() :: registered_name() | {registered_name(), node()}.
-type monitor_process_identifier() :: pid() | registered_process_identifier().

So is the type of Mon pid() | atom() | {atom(), node()}?


Solution

  • Mon is the reference of the monitor:

    monitor_return({{ok, Pid}, Mon}) when is_pid(Pid), is_reference(Mon) ->
        %% Successful start_monitor()...
        {ok, {Pid, Mon}};
    

    (code is here)