why doesn't this erlang code work?

最后都变了- 提交于 2019-12-06 06:00:58

In your conFib you send an integer, but await a tuple in rpc. Should change it to:

conFib()->
       receive
               {Client,N} -> Client ! {self(), regfib(N)}
       end.

You can evade such situations by using timeout with after in your receives.

To make the computation parallel you could do something like:

fib(N)->
       P1 = spawn(fun test:conFib/0),
       P2 = spawn(fun test:conFib/0),
       P1 ! {self(), N - 2},
       P2 ! {self(), N - 1},
       receive
         {_, R1} -> R1
       end,
       receive
         {_, R2} -> R2
       end,
        R1 + R2.

The important part is that you send both of the requests before waiting for the answers. The part waiting for the answers could of course be done in a more beautiful way.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!