Here is a puzzle for you guys. I have a multi-threaded program in which some threads work with managed resources, such as locks and semaphores. Some of the blocking release primitives can only be executed from the same thread on which the lock was locked.
So, here is my puzzle: I wrap these operations: try {lock-acquire ... do something} finally {lock-release}, but sometimes, when my threads finish, finally sentences are executed by the .NET garbage collection stream, and not my stream. (In a specific case, the Dispose of the object selected in the "using" statement is actually used, see below for details)
This is a little tricky to demonstrate; I see this all the time in Isis2, and I realized that this was happening by checking the flow identifiers in the acquisition and completion blocks. But I don't have a 3 line demo for you, and I'm sorry about that. I know this would make it easier to help.
Is there a way that you can delay completion for a thread until all pending completion blocks associated with this thread are executed?
---- Added information for Brand ----
What I'm really doing is related to a rather complicated self-service blocking package, which has various blocking abstractions (limited buffers, barriers, normal locks, etc.) with thread priorities and is intended for self-detection of deadlocks, priority inversions, very slow blocking and other problems. My code is quite complex and streamy enough, so I had to debug it.
An example of my basic design style is the following:
LockObject myLock = new LockObject("AnIsis2Lock"); ... using(new LockAndElevate(myLock)) { stuff }
LockObject is a self-locking lock ... LockAndElevate notes that I locked it (in this case) and then tracks the deletion when I unlock it. Therefore, I use the fact that use should destroy a new object when the block completes, even if it throws an exception.
What I see is that quite often threads terminate, and yet the use of dispose events did not actually happen; they start later, on another thread. This only happens when one of my threads terminates; in normal execution, everything works like a charm.
So, since the translation is used to try ... finally, my question was published in terms of final sentences.