what is ref in Erlang?

醉酒当歌 提交于 2019-12-19 08:31:01

问题


Going through this link

To identify a process,we would be using a Pid.But when should a ref be used?

I see this often while msg sending/receiving but unable to interpret what role it plays in message interaction.


回答1:


To quote documentation

A reference is a term which is unique in an Erlang runtime system, created by calling make_ref/0.

This means that this is special data type, it's not an integer, not a list, and not a binary. Especially with unique prosperity. It's designed mostly to recognize some places in code. make_ref(), no matter where it is called, will return new value (in boundaries of it's size of course). And just like Fred describes in it's book, it is great for tagging messages, and recognizing if message we receive is in response to one we just send.




回答2:


In complement to other responses, beware that a ref:

  • is not a random value (just start a VM an type make_ref(). several times)
  • is unique only in one VM (open a new VM and type make_ref() several times, you will get the same result as above) Nevertheless you can create one using a tuple {node(),make_ref()}.

No discrepancy with the documentation, but I made some wrong assumption in the past ... :o). Thanks @Robert for correction!




回答3:


Disclaimer: My answer is mostly based on Learn You Some Erlang, in particular the link provided in the question.


The idea of using references is to uniquely identify a given message, so that you can match the messages and their responses - given that the response includes the reference sent in the original message.

We need to identify messages when using named processes that restart because, in this scenario, we can't rely on PIDs, due to concurrency. As is given in the book:

  1. critic ! Message
  2. critic receives
  3. critic replies
  4. critic dies
  5. whereis fails
  6. critic is restarted
  7. code crashes

or

  1. critic ! Message
  2. critic receives
  3. critic replies
  4. critic dies
  5. critic is restarted
  6. whereis picks up wrong pid
  7. message never matches

There are interleavings where you'll get the wrong PID, or no PID at all, leaving your process unable to receive a response, if it was expecting a response of the form {Pid, Response}. This is a consequence of using named processes that restart: you know their PID might change without notice, unlimited times, hence naming them as a convenience to send them messages.

Using a unique identifier to tag the message avoids this problem altogether, because you expect the reference to be returned in the response, not a PID.

This question might explain why I use the italics in unique. The references are not really unique, but unique enough for practical purposes.



来源:https://stackoverflow.com/questions/26337647/what-is-ref-in-erlang

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