this is code:
class program
{
static ReaderWriterLockSlim rwLock = new ReaderWriterLockSlim();
static void Main(string[] args)
{
for (int i = 0; i < 2; i++)
{
Task.Factory.StartNew(Read);
}
for (int i = 0; i < 1; i++)
{
Task.Factory.StartNew(Write);
}
Console.Read();
}
static void Read()
{
while (true)
{
rwLock.EnterReadLock();;
Thread.Sleep(100);
Console.WriteLine($"{Thread.CurrentThread.ManagedThreadId} reading");
rwLock.ExitReadLock();
}
}
static void Write()
{
while (true)
{
rwLock.EnterWriteLock();
Thread.Sleep(3000);
Console.WriteLine($"{Thread.CurrentThread.ManagedThreadId} writing");
rwLock.ExitWriteLock();
}
}
}
I want to test the ReaderWriterLockSlim, the result what I expect is read and write alternately in console, but at last, there only write in console.
Here the result:
5 reading 2020/2/17 18:24:36
4 reading 2020/2/17 18:24:36
3 reading 2020/2/17 18:24:36
6 writing 2020/2/17 18:24:39
6 writing 2020/2/17 18:24:42
6 writing 2020/2/17 18:24:45
6 writing 2020/2/17 18:24:48
6 writing 2020/2/17 18:24:51
6 writing 2020/2/17 18:24:54
6 writing 2020/2/17 18:24:57
6 writing 2020/2/17 18:25:00
6 writing 2020/2/17 18:25:03
6 writing 2020/2/17 18:25:06
but when use ReaderWriterLock, its alternate display. Here the result:
4 reading 2020/2/17 18:30:22
3 reading 2020/2/17 18:30:22
5 reading 2020/2/17 18:30:22
6 writing 2020/2/17 18:30:25
4 reading 2020/2/17 18:30:25
3 reading 2020/2/17 18:30:25
5 reading 2020/2/17 18:30:25
6 writing 2020/2/17 18:30:28
4 reading 2020/2/17 18:30:29
5 reading 2020/2/17 18:30:29
3 reading 2020/2/17 18:30:29
6 writing 2020/2/17 18:30:32
4 reading 2020/2/17 18:30:32
5 reading 2020/2/17 18:30:32
3 reading 2020/2/17 18:30:32
6 writing 2020/2/17 18:30:35
5 reading 2020/2/17 18:30:35
3 reading 2020/2/17 18:30:35
4 reading 2020/2/17 18:30:35
6 writing 2020/2/17 18:30:38
5 reading 2020/2/17 18:30:45
3 reading 2020/2/17 18:30:45
4 reading 2020/2/17 18:30:45
Why does this happen, someone tell me the reason for this phenomenon? Hope answer
The problems here is that thread.Sleep() is called whilst inside the critical section. this causes the writer thread to hold the lock for periods of 3 secs each time and releases it for a very small amount of time in which the writer thread re-acquires the lock as, in this case apparently, ReaderWriterLockSlim gives better priority to writers.
I tried calling Thread.Sleep() after releasing the lock and everything works as expected.
It's worth checking out the .Net reference source for ReaderWriterLockSlim and ReaderWriterLock