Passing a lock to another thread #119555
Replies: 2 comments 1 reply
-
Stupid alternate solution:
The lock must be locked. There is no reason this method would need to check if it's being called by two threads at once. |
Beta Was this translation helpful? Give feedback.
-
.... like what?
This is a recipe for deadlocks, because you could be sending the lock to a thread about to exit/crash/whatever, and now the lock is "lost" (and under no circumstances would a thread be able to "steal" a lock from whichever one already owns it, because that would be a recipe for an even larger disaster). It's somewhat unusual to work directly with In any case, lock handoffs between threads like you're suggesting are incredibly suspicious, implying that you're probably doing something wrong. |
Beta Was this translation helpful? Give feedback.
Uh oh!
There was an error while loading. Please reload this page.
Uh oh!
There was an error while loading. Please reload this page.
-
ReaderWriterLock implements IDisposable
ReaderWriterLockSlim implements IDisposable
I got myself into a place where this is massively inconvenient. So I wrote an implementation of a reader writer lock in terms of the managed locking primitives. It didn't work.
The reason it doesn't work may be understood as follows:
As a base check, I did the following:
That code ran through. There's no reason a priori why the method cannot function; but it won't function because the caller checks.
You're probably thinking this doesn't work anyway because the lock is recursive. In my case I can prove the second call to Enter isn't recursive.
Note that just knocking the check out is a bad idea. We would have to have more like a constructor argument to say "make a non-recursive lock that doesn't check".
Beta Was this translation helpful? Give feedback.
All reactions