-
Notifications
You must be signed in to change notification settings - Fork 0
/
kitchen.erl
51 lines (45 loc) · 960 Bytes
/
kitchen.erl
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
-module(kitchen).
-compile(export_all).
fridge1(FoodList) ->
receive
{From,{store, Food} } ->
From ! {self(), ok},
fridge1([Food|FoodList]);
{From , {take, Food}} ->
case lists:member(Food, FoodList) of
true ->
From ! {self(), {ok, Food}},
fridge1(lists:delete(Food, FoodList));
false ->
From ! {self(), {ok, not_found}},
fridge1(FoodList)
end;
terminate ->
ok
end.
store(Pid, Food) ->
Pid ! {self(), {store, Food}},
receive
{Pid, Msg} -> Msg
end.
take(Pid, Food) ->
Pid ! {self(), {take, Food} },
receive
{Pid, Msg} -> Msg
end.
start(FoodList) ->
spawn(?MODULE, fridge1, [FoodList]).
store2(Pid, Food) ->
Pid ! {self(), {store, Food}},
receive
{Pid, Msg} -> Msg
after 3000 ->
timeout
end.
take2(Pid, Food) ->
Pid ! {self(), {take, Food}},
receive
{Pid, Msg} -> Msg
after 3000 ->
timeout
end.