-
Hi, #r "nuget: DotNext.Threading, 5.4.0"
#r "nuget: DotNext, 5.3.1"
using System.Threading;
using DotNext.Threading;
await using var rwlock = new AsyncReaderWriterLock();
await Parallel.ForAsync(0,50, async (i,t)=>{
using (await rwlock.AcquireReadLockAsync(t))
{
i.Dump();
Thread.Sleep(i*10);
rwlock.IsReadLockHeld.Dump("read");
await rwlock.UpgradeToWriteLockAsync(t); //waits infinite
try {
rwlock.IsWriteLockHeld.Dump("write");
Thread.Sleep(i*10);
}
finally {
rwlock.DowngradeFromWriteLock();
}
}
}); |
Beta Was this translation helpful? Give feedback.
Answered by
sakno
May 28, 2024
Replies: 1 comment
-
Write lock is exclusive lock. Each async flow has acquired read lock. You cannot upgrade to write lock while you have active read locks. |
Beta Was this translation helpful? Give feedback.
0 replies
Answer selected by
oruchreis
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Write lock is exclusive lock. Each async flow has acquired read lock. You cannot upgrade to write lock while you have active read locks.