The challenge for synchronization. Why is it the priority of the reader?

Need to ask equitable access and readers and writers.
Can't understand what's wrong in my version.

using System;
using System.Threading;

namespace rw_ReaderWriterLockSlim
{
 class Program
{
 rwLockSlim static ReaderWriterLockSlim = new ReaderWriterLockSlim();
 static bool one = true;
 static int n = 0;

 public static void Count(object obj)
{
 one = false;
}

 public static void WriterFunction()
{
 while (one)
{
rwLockSlim.EnterReadLock();
Console.WriteLine(Thread.CurrentThread.Name);
n++;
rwLockSlim.ExitReadLock();

}
rwLockSlim.EnterWriteLock();
}

 public static void ReaderFunction()
{
 while (one)
{
rwLockSlim.EnterWriteLock();
Console.WriteLine(Thread.CurrentThread.Name);
n++;
rwLockSlim.ExitWriteLock();

}
rwLockSlim.EnterReadLock();
}

 static void Main()
{

 for (int i = 0; i < 64; i++)
{
 Thread myThread = new Thread(ReaderFunction);

 myThread.Name = i.ToString() + "th READ";
myThread.Start();
}
 for (int i = 0; i < 4; i++)
{
 Thread myThread = new Thread(WriterFunction);
 myThread.Name = i.ToString() + "minutes recorded";
myThread.Start();
}


 Timer timer = new Timer(Count, null, 1000, 1000);
Console.ReadLine();
Console.WriteLine(n);
Console.ReadLine();
}
}
}
April 3rd 20 at 18:38
1 answer
April 3rd 20 at 18:40
Why is it the priority of the reader?

How do you understand this? What is the priority? What is equitable access? And then sync?
The point is to alternate work flows. In the console should be a turn to Read, he wrote.
The function of the reader, I block write access, and I have READ, free access and the block read access. Then turn to the function of the writer. There similarly.
My code first displays all the readers, and in late writers. - loraine commented on April 3rd 20 at 18:43
@loraine,
to alternated work flows. In the console should be output in turn

This is called serial reading and writing. It's NOT the threads. Streams is when reading and writing happen in parallel.
In your case you should write so:
ReaderFunction();
WriterFunction();
ReaderFunction();
WriterFunction();
...

Or raise the flag whose turn it is. Something like:
ReaderFunction(){
while(!CanRead) sleep(1);
read();
CanRead=false;}
- crystel74 commented on April 3rd 20 at 18:46

Find more questions by tags C#