receive ... after en Erlang
after le pone reloj al receive.
%% after puts a clock on receive: no message, take the timeout arm.
main(_) ->
%% Nobody ever calls, so the timeout arm answers.
receive
{call, Line} -> io:format("Ring on ~p~n", [Line])
after 50 ->
io:format("Quiet board, going to lunch~n")
end,
%% A message already waiting wins over the clock.
self() ! {call, 3},
receive
{call, L} -> io:format("Caught line ~p in time~n", [L])
after 1000 ->
io:format("Never reached~n")
end.
Cómo funciona
- Sin mensaje que matchee al plazo: corre el brazo after.
- Un mensaje en espera siempre le gana al reloj.
self() ! Msgencola un mensaje al buzón propio.
Palabras clave y builtins usados aquí
afterendreceive
El intento, en números
- Líneas
- 16
- Caracteres a escribir
- 429
- Tokens
- 94
- Ritmo de tres estrellas
- 60 tpm
Al ritmo de tres estrellas de 60 tokens por minuto, este intento toma unos 94 segundos.
Paso 2 de 4 en Procesos; paso 26 de 31 en Fundamentos del lenguaje.