title: How to give different priorities to erlang messages
body: <p>In the book "erlang programming" and in <a href="https://stackoverflow.com/questions/8699498/is-there-a-clever-way-to-give-messages-different-priorities">this</a> stack overflow question I have seen that we can give different priorities to erlang messages in the mailbox using:</p>
  
  <p>I tried to implement something similar with this code:</p>
  
  <pre><code>-module(mytest).
  -export([start/0, test/0]).
  
  start() -&gt;
      register(?MODULE, spawn(fun() -&gt; do_receive() end)).
  
  do_receive() -&gt;
      receive
          {high_p, Message} -&gt;
              io:format("High priority: ~p~n", [Message]),
              do_receive()
      after 0 -&gt;
              receive
                  {low_p, Message} -&gt;
                      io:format("Low priority: ~p~n", [Message]),
                      do_receive()
              end
      end.
  
  test() -&gt;
      mytest ! {high_p, msg1},
      mytest ! {low_p, msg2},
      mytest ! {high_p, msg3}.
  </code></pre>
  
  <p>But the result was:</p>
  
  <pre><code>1&gt; mytest:start().
  true
  2&gt; mytest:test(). 
  Low priority: msg2
  {high_p,msg3}
  High priority: msg1
  High priority: msg3
  </code></pre>
  
  <p>Which seems not right, so I changed the code as:</p>
  
  <pre><code>do_receive() -&gt;
      receive
          {high_p, Message} -&gt;
              io:format("High priority: ~p~n", [Message]),
              do_receive()
      after 0 -&gt;
              receive
                  {high_p, _} = Other -&gt;
                      self() ! Other,
                      do_receive();
                  {low_p, Message} -&gt;
                      io:format("Low priority: ~p~n", [Message]),
                      do_receive()
              end
      end.
  </code></pre>
  
  <p>and the result is the following (all high priority messages are printed before the low priority ones). </p>
  
  <pre><code>1&gt; mytest:start().
  true
  2&gt; mytest:test(). 
  High priority: msg3
  {high_p,msg3}
  High priority: msg1
  Low priority: msg2
  </code></pre>
  
  <p>Is this the correct way in order to achieve my goal? And can different priorities be achieved within gen_server handle_info/2 ?</p>