Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System;
- using System.Threading;
- using System.IO;
- class App
- {
- static ReaderWriterLockSlim rw = new ReaderWriterLockSlim();
- static int reads = 0;
- static int writes = 0;
- static void Main()
- {
- for (int i = 0; i < 3; ++i)
- {
- new Thread(Reader).Start();
- new Thread(Writer).Start();
- }
- while(true)
- {
- Thread.Sleep(1000);
- int r = reads;
- int w = writes;
- Console.WriteLine("{0}\t{1}\r", r, w);
- }
- }
- static void Reader()
- {
- while(true)
- {
- double x = 0;
- rw.EnterReadLock();
- Interlocked.Increment(ref reads);
- for(int i = 1; i < 10; ++i) x += Math.Sqrt(i);
- rw.ExitReadLock();
- }
- }
- static void Writer()
- {
- while(true)
- {
- double x = 0;
- rw.EnterWriteLock();
- var w = Interlocked.Increment(ref writes);
- for(int i = 1; i < 10; ++i) x += Math.Sqrt(i);
- rw.ExitWriteLock();
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement