《Erlang程序设计》第8章习题第一…

(1)编写一个函数start(AnAtom,Fun)来把spawn(Fun)的结果注册为AnAtom。当两个并行的进行同时执行到start/2函数时,要确保代码能够正常工作。也就是说,这两个进程其中一个成功执行,而另一个必须执行失败。

-module(p150_1_process_conflict).
-export([main/0]).

%Not to concern about what the function Fun is.

main() -> %% ctreate two process.
start(wrong111,(fun() -> io:format("Wrong~n") end)),
%�d something to make sure there are not system output
start(ok111,(fun() -> io:format("Ok~n") end)).

start(AnAtom, Fun) ->  
      R = self(),
      spawn(fun() -> try register(AnAtom, R) of  
%%start should be registered in order to avoid two processes execute start/2 in the same time.
         
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值