[Shootout-list] Producer/consumer using processes?

Einar Karttunen ekarttun@cs.helsinki.fi
Sun, 13 Jun 2004 09:11:11 +0300


Hello

Erlang has no concept of threads, just processes that communicate
by passing messages to each other. Would implementing the
producer/consumer test with message passing be ok?

This is an incomplete example of how this would work, does
implementing it make sense?

producer(0) -> ok;
producer(N) ->
        receive
                {consumer_ready, Pid} -> 
                        Pid ! {producer_data, N},
                        producer(N-1)
        end.

consumer(Producer) ->
        Producer ~ {consumer_ready, self()},
        receive
                {producer_data, 10000} -> ok;
                {producer_data, Data} -> consumer(Producer)
        end.

ps. Tests that take longer and are thus not related to startup time
would be nice. And the producer/consumer would make sense with 100
producers ;)

- Einar Karttunen