Erlang (programming language)/Tutorials/Timeouts

From Citizendium
< Erlang (programming language)‎ | Tutorials
Revision as of 07:07, 8 August 2009 by imported>Tom Morris (Erlang programming language/Tutorials/Timeouts moved to Erlang (programming language)/Tutorials/Timeouts)
(diff) ← Older revision | Latest revision (diff) | Newer revision → (diff)
Jump to navigation Jump to search

Timeouts

Timeouts are created by the [ receive - after - end ] structure. Timeouts are measured in miliseconds so 4000 = 4 seconds. We can create a simple timer with the following program: myTimer.erl

% ============================================================ >%
-module( myTimer ).
-compile( export_all ).
                                                                %
% A simple timer that uses a timeout.
                                                                %
start( Timeout ) ->
    receive
    after Timeout ->
        io:format( "your ~w secs are up." , [Timeout/1000] )
    end.	
% ============================================================ >%
%
% Sample output:
% 
% 8> c(myTimer).
% ok.
%
% 9> myTimer:start(4000).
% your 4.00000 secs are up.ok

The atom:

infinity

is a valid value for a timeout and causes an infinite wait.