[erlang-questions] how to handle a complex timer process ?

Chad DePue chad@REDACTED
Mon Jun 14 20:15:56 CEST 2010


I'd use a gen_fsm for this, then you can handle the timeout message in each
state and transition appropriately.

Chad DePue
skype: cdepue
inakanetworks.com - Erlang consulting
rubyrescue.com - Ruby on Rails consulting



On Mon, Jun 14, 2010 at 2:10 PM, info <info@REDACTED> wrote:

> Hi all,
> I want to manage a timer with several actions.
> - creation: creation of the timer process with "Period" as parameter. The
> timer shall be in suspend state.
> - start: the timer is started.
> - restart: the timer restarts with the Period.
> - stop: the timer is stopped. The timer shall be in wait state. A start
> starts the timer.
> - kill: the timer process is killed.
> - after the timeout, a message is sent to another process. The timer shall
> be in suspend state.
>
> For the creation, I do:
> Pid = spawn (fun,?MODULE,the_timer,[Period]),
> and after I manage like this:
> Pid ! {start}
> Pid ! {stop}
> Pid ! {restart}
> Pid ! {kill}
>
> The structure of the timer is like this:
>
> the_timer(Period)->
>    receive
>        {start}-> ???
>        {restart}->
>            the_timer(Period);
>        {stop}-> ???
>        {kill}-> ???
>    after Period ->
>        Pid0 -> {one_message},
>        ???
>    end.
>
> How to handle the state ? how to keep the time after a stop and to start
> again with this time ? how to kill the process ?
> How to start the timer only if a start message is received ?
>


More information about the erlang-questions mailing list