Created
December 6, 2019 22:44
-
-
Save anhnguyen1618/53272b8b20ea5413bd6091dd8d8d59bf to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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