lecture 07

This commit is contained in:
SowinskiBraeden committed 2026-10-20 11:28:07 -07:00
1 parent d729786437
commit 73653f078e
7 files changed
+165

No files matched your search

+30
View File
@@ -0,0 +1,30 @@
-module(counter_worker).
-behaviour(gen_server).
-export([init/1, handle_call/3, handle_cast/2]).
-export([start_link/1, inc/1, value/0]).
-define(SERVER, ?MODULE).
-define(TABLE, counter_table).
start_link(N) ->
gen_server:start_link({local, ?SERVER}, ?MODULE, N, []).
inc(Amt) ->
gen_server:cast(?SERVER, {inc, Amt}).
value() ->
gen_server:call(?SERVER, value).
init(N) ->
case ets:lookup(?TABLE, value) of
[] -> {ok, N};
[{_, M} | _] -> {ok, M}
end.
handle_call(value, _From, N) ->
{reply, N, N}. % {reply, actual_reply, state}
handle_cast({inc, Amt}, N) ->
{noreply, N + Amt}.
termintate(_Reason, N) ->
ets:insert(?TABLE, {value, N}).