title: Is there any way to lock variable between different process in Erlang
body: <p>There's variable in my module, and there's receive method to renew variable value. And multiple process are calling this method simultaneously. I need lock this variable when one process is modifying it. Sample as below</p>
  
  <p>mytest.erl</p>
  
  <pre><code>%%%-------------------------------------------------------------------
  -module(mytest).
  
  %% API
  -export([start_link/0,display/1,callDisplay/2]).
  
  start_link()-&gt;
    Pid=spawn(mytest,display,["Hello"]),
    Pid.
  
  display(Val) -&gt;
    io:format("It started: ~p",[Val]),
    NextVal=
      receive
        {call,Msg}-&gt;
          NewVal=Val++" "++Msg++" ",
          NewVal;
        stop-&gt;
          true
      end,
    display(NextVal).
  
  callDisplay(Pid,Val)-&gt;
    Pid!{call,Val}.
  </code></pre>
  
  <p>Start it</p>
  
  <pre><code>Pid=mytest:start_link().
  </code></pre>
  
  <p>Two process are calling it in the same time</p>
  
  <pre><code>P1=spawn(mytest,callDisplay,[Pid,"Walter"]),
  P2=spawn(mytest,callDisplay,[Pid,"Dave"]).
  </code></pre>
  
  <p>I hope it can add "Walter", "Dave" one by one like "Hello Walter Dave", however, when there're too many of them running together, some Names(Walter, Dave, etc) will be override.</p>
  
  <p>Because when P1, P2 started the same time, Val both are "Hello". P1 add "Walter" to become "Hello Walter", P2 add "Dave" to become "Hello Dave". P1 saved it firstly to NextVal as "Hello Walter", then P2 saved it to NextVal as "Hello Dave", so result will be "Hello Dave". "Hello Walter" is replaced by "Hello Dave", and "Walter" lost forever.</p>
  
  <p>Is there any way I can lock "Val", so when we add "Walter", "Dave" will waiting till Value setting is done?</p>