Example #1
0
        /*
         * Locks first string object in array, and pops it off list, before recursively calling self, until
         * no more objects remains. When all objects are unlocked, then it will execute given "functor" delegate
         */
        static void WriteLock(List <string> lockers, LockFunctor functor)
        {
            // Checking if there are any more lockers to wait for.
            if (lockers.Count == 0)
            {
                // No more lockers to wait for, evaluating lambda.
                functor();
            }
            else
            {
                // Retrieves next locker, and locks it as a write lock.
                var locker = GetLocker(lockers [0]);
                locker.EnterWriteLock();
                try {
                    // Removing current lock from list.
                    lockers.RemoveAt(0);

                    // Recursively invoking "self", now with one locker less in list of lockers to wait for.
                    WriteLock(lockers, functor);
                } finally {
                    // Opening locker.
                    locker.ExitWriteLock();
                }
            }
        }
Example #2
0
        /*
         * Locks first string object in array, and pops it off list, before recursively calling self, until
         * no more objects remains. When all objects are unlocked, then it will execute given "functor" delegate
         */
        private static void LockNext(List <string> lockers, LockFunctor functor)
        {
            // Checking if there are any more lockers to wait for
            if (lockers.Count == 0)
            {
                // No more lockers to wait for, evaluating lambda
                functor();
            }
            else
            {
                // Retrieves next locker, and locks it again, once it is unlocked
                lock (GetLocker(lockers [0])) {
                    // Removing current locker from list
                    lockers.RemoveAt(0);

                    // Recursively invoking "self", now with one locker less in list of lockers to wait for
                    LockNext(lockers, functor);
                }
            }
        }