C # writer writer writing (selfish reader)

xiaoxiao2021-03-06  68

// self-privacy reader's writer write system; use system.threading; public class book {private int [] pages; private object mutex = new object (); // Control to ReaderCount Visit Private Object DB = New Object ( ); // private int control access to pages of readerCount = 0; public Book (int numOfPages) {pages = new int [numOfPages];} public int numOfPages {get {return pages.Length;}} public int this [int index ] {GET {Lock (Mutex) {// Check if it is the first reader ReaderCount ; if (ReaderCount == 1) Monitor.Enter (DB);} int valuecopy = pages [index]; // Read a data console .Writeline (thread.currentthread.name "read the" (INDEX 1) page, the data he read is " valuecopy); DisplayContent (); lock (mutex) {readercount--; IF Readercount == 0) Monitor.exit (db);} return valuecopy;} set {lock (db) {pages [index] = value; console.writeline (thread.currentthread.name "Write" Value " " (INDEX 1) " page "); displaycontent ();}}} private void displayContent () {console.writeline (" {0, -35} "," book content is: "); for (int i = 0; i

转载请注明原文地址:https://www.9cbs.com/read-109626.html

New Post(0)