Mutex: Difference between revisions

Content deleted Content added
Added Erlang
Line 277: Line 277:


<code>when</code> blocks and <code>Ref.whenResolved</code> return a ''promise'' for the result of the deferred action, so the mutex here waits for the gratuitously complicated increment to complete before becoming available for the next action.
<code>when</code> blocks and <code>Ref.whenResolved</code> return a ''promise'' for the result of the deferred action, so the mutex here waits for the gratuitously complicated increment to complete before becoming available for the next action.

=={{header|Erlang}}==
Erlang has no mutexes so this is a super simple one, hand built to allow 3 slowly printing processes to print until done before the next one starts.
<lang Erlang>
-module( mutex ).

-export( [task/0] ).

task() ->
Mutex = erlang:spawn( fun() -> loop() end ),
[erlang:spawn(fun() -> random:seed( X, 0, 0 ), print(Mutex, X, 3) end) || X <- lists:seq(1, 3)].



loop() ->
receive
{acquire, Pid} ->
Pid ! {access, erlang:self()},
receive
{release, Pid} -> loop()
end
end.

mutex_acquire( Pid ) ->
Pid ! {acquire, erlang:self()},
receive
{access, Pid} -> ok
end.

mutex_release( Pid ) -> Pid ! {release, erlang:self()}.

print( _Mutex, _N, 0 ) -> ok;
print( Mutex, N, M ) ->
timer:sleep( random:uniform(100) ),
mutex_acquire( Mutex ),
io:fwrite( "Print ~p: ", [N] ),
[print_slow(X) || X <- lists:seq(1, 3)],
io:nl(),
mutex_release( Mutex ),
print( Mutex, N, M - 1 ).

print_slow( X ) ->
io:fwrite( " ~p", [X] ),
timer:sleep( 100 ).
</lang>
{{out}}
<pre>
27> mutex:task().
Print 2: 1 2 3
Print 1: 1 2 3
Print 3: 1 2 3
Print 2: 1 2 3
Print 1: 1 2 3
Print 3: 1 2 3
Print 2: 1 2 3
Print 1: 1 2 3
Print 3: 1 2 3
</pre>


=={{header|Go}}==
=={{header|Go}}==