Skip to content

Instantly share code, notes, and snippets.

@alinpopa
Created August 2, 2010 05:32
Show Gist options
  • Save alinpopa/504157 to your computer and use it in GitHub Desktop.
Save alinpopa/504157 to your computer and use it in GitHub Desktop.
-module(pc_queue).
-export([start/0,produce/2,consume/1,status/1]).
start() ->
Queue = queue:new(),
spawn(fun() -> loop(Queue) end).
loop(Q) ->
receive
{produce,Msg} ->
io:format("Produce msg: ~p~n",[Msg]),
NewQ = queue:in(Msg,Q),
loop(NewQ);
consume ->
case queue:out(Q) of
{{value,Msg}, NewQ} ->
io:format("Consume msg: ~p~n",[Msg]),
loop(NewQ);
{empty,_} ->
io:format(" -> no msg in queue~n"),
loop(Q)
end;
status ->
io:format("QUEUE: ~p~n",[Q]),
loop(Q);
Command ->
io:format(" -> ERROR: invalid command: ~p~n",[Command]),
loop(Q)
end.
produce(QPid, Msg) ->
QPid ! {produce, Msg}.
consume(QPid) ->
QPid ! consume.
status(QPid) ->
QPid ! status.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment