Advertisement
Guest User

Untitled

a guest
Jun 24th, 2017
58
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Erlang 0.63 KB | None | 0 0
  1. -module(semaphore).
  2. -compile(export_all).
  3.  
  4. start()->
  5.     Lock = spawn(?MODULE, init, []),
  6.         spawn(?MODULE, peer, ["this", Lock]),
  7.         spawn(?MODULE, peer, ["andthat", Lock]).
  8.  
  9. p(Lock) ->
  10.     semaphore ! {p, Lock},
  11.     receive
  12.         go ->
  13.             go
  14.     end.
  15.  
  16. v(Lock) ->
  17.     semaphore ! {v, Lock},
  18.     ok.
  19.  
  20. init() ->
  21.     unlocked().
  22.  
  23. unlocked() ->
  24.     receive {p, Lock} ->
  25.             Lock ! go,
  26.             locked(Lock)
  27.     end.
  28.  
  29. locked(Lock) ->
  30.     receive
  31.         {v, Lock} ->
  32.             unlocked()
  33.     end.
  34.  
  35. peer(Text, Lock) ->
  36.     p(Lock),
  37.     io:format("~w~n", [Text]),
  38.     io:format("~w~n", [Text]),
  39.     io:format("~w~n", [Text]),
  40.     v(Lock),
  41.     peer(Text, Lock).
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement