blog.pauloasilva.com

Um 2009 cheio de concorrência

Inspirado no repto lançado pela Adobe, deixo os meus votos para o ano de 2009 em 26 linhas de pura concorrência ;)

-module(newYear).
-export([start/0,newLife/0,happiness/0,health/1,work/0]).
start() -> spawn(newYear,newLife,[]).
newLife() -> spawn(newYear,happiness,[]).
happiness() ->
    WorkPid = spawn(newYear,work,[]),
    HealthPid = spawn(newYear,health,[WorkPid]),
    HealthPid ! go.
health(WorkPid) ->
    receive
        go ->
            io:format("Healthy..."),
            WorkPid ! {self(),go},
            health(WorkPid);
        ill ->
            io:format("I'm ill..."),
            WorkPid ! {self(),stop}
    end.
work() ->
    receive
        {HealthPid,go} ->
            io:fwrite(" and working~n"),
            HealthPid ! go,
            work();
        stop -> io:format(" so I'll stop working!~n")
    end.

One comment

  1. Arsénio diz:

    Eheheh só vi este post agora colega !
    Concurrencia pura !
    Respeito pelo CPU zero :)

Deixar uma resposta

O seu endereço de email não será publicado. Campos obrigatórios marcados com *

*


7 − four =

Pode usar estas etiquetas HTML e atributos: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong> <pre lang="" line="" escaped="" highlight="">