erlang之exit/1,exit/2探索

来源:互联网 发布:淘宝网泳装 编辑:程序博客网 时间:2024/06/05 03:51
-module(time).  
-export([start/2, stop/0, stop/1, cancel/1]).  
start(Time,Fun) ->  
     spawn(fun() -> time(Time,Fun) end).  
    
stop(Pid) ->              
     %%exit(Pid, normal).        %% NO
     %% If Reason is the atom normal, Pid will not exit.
     %% If it is trapping exits, the exit signal is
     %% transformed into a message {'EXIT', From, normal}
     %% and delivered to its message queue.
    
    
     exit(Pid, kill).                       %%YES    
     %%  If Reason is the atom kill, that is if
     %%  exit(Pid, kill) is called, an untrappable
     %%     exit signal is sent to Pid which will
     %%  unconditionally exit with exit reason killed.
    
stop() ->           %% YES 停掉调用time:stop()的进程,达不到停止要停止的进程的要求,是否normal无关
     %%exit(normal).
     exit(shutdown).
    
cancel(Pid) ->  
     Pid ! cancel.  
      
time(Time,Fun) ->  
     receive cancel ->  
         void  
     after Time ->  
         Fun(),
         %%stop(),                     %% OK
         %%exit(normal),          %% OK
         time(Time,Fun)
    end.  
    
timer(Time,Fun) ->  
     Fun(),
     timer:sleep(Time),
     io:format("here~n"),
     %%stop(),                        %% OK
     %%exit(normal),             %% OK
     time(Time,Fun).

原创粉丝点击