Can I use async and sync lock on same AsyncReaderWriterLock ? #441
-
Hi, private static readonly AsyncReaderWriterLock _rwlock = new();
//Thread:1 with async using
using (await _rwlock.WriterLockAsync(token))
{}
//Thread:2 with sync using
using (_rwlock.UpgradeableReaderLock())
{
using(_rwlock.WriterLock())
{}
} |
Beta Was this translation helpful? Give feedback.
Replies: 2 comments
-
Hi @oruchreis, sorry I didn't see this until now (I usually get notifications on this repo, not sure why I didn't for this discussion). Yes, |
Beta Was this translation helpful? Give feedback.
-
Btw, your upgradeable lock example is used incorrectly. It should look like this: //Thread:2 with sync using
using (var upgradeableReaderKey = _rwlock.UpgradeableReaderLock())
{
using(_rwlock.UpgradeToWriterLock(upgradeableReaderKey))
{}
} Otherwise, your normal |
Beta Was this translation helpful? Give feedback.
Hi @oruchreis, sorry I didn't see this until now (I usually get notifications on this repo, not sure why I didn't for this discussion).
Yes,
AsyncReaderWriterLock
is designed to work with both async and sync on the same instance. It's not really recommended due to deadlock issues, but if you know for a fact that the sync calls will only happen on their own thread, that's exactly the situation it was designed to support.