Skip to content

Instantly share code, notes, and snippets.

@anhnguyen1618
Created December 6, 2019 22:44
Show Gist options
  • Save anhnguyen1618/53272b8b20ea5413bd6091dd8d8d59bf to your computer and use it in GitHub Desktop.
Save anhnguyen1618/53272b8b20ea5413bd6091dd8d8d59bf to your computer and use it in GitHub Desktop.
readers-writers : monitor;
begin
readercount : integer;
busy : boolean;
OKtoread, OKtowrite : condition;
procedure startread;
begin
if busy then OKtoread.wait;
readercount := readercount + 1;
OKtoread.signal;
(* Once one reader can start, they all can *)
end startread;
procedure endread;
begin
readercount := readercount - 1;
if readercount = 0 then OKtowrite.signal;
end endread;
procedure startwrite;
begin
if busy OR readercount != 0 then OKtowrite.wait;
busy := true;
end startwrite;
procedure endwrite;
begin
busy := false;
if OKtoread.queue then OKtoread.signal
else OKtowrite.signal;
end endwrite;
begin (* initialization *)
readercount := 0;
busy := false;
end;
end readers-writers;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment