public T this[int index]
 {
     get
     {
         T result = default(T);
         _lock.ExecuteRead(() => result = _list[index]);
         return(result);
     }
 }
Beispiel #2
0
        public void ExecuteWrite()
        {
            var s = "test";

            m_lock.ExecuteWrite(() =>
            {
                s           = "Acquired Write";
                bool result = SyncExecuteOnOtherThread(() => m_lock.ExecuteRead(() => { throw new Exception("Read was allowed!"); }, TimeSpan.FromMilliseconds(200)));
                result.ShouldBeEquivalentTo(false);
                result = SyncExecuteOnOtherThread(() => m_lock.ExecuteWrite(() => { throw new Exception("Write was allowed!"); }, TimeSpan.FromMilliseconds(200)));
                result.ShouldBeEquivalentTo(false);
                result = SyncExecuteOnOtherThread(() => m_lock.ExecuteUpgradableRead(() => { throw new Exception("Write was allowed!"); }, TimeSpan.FromMilliseconds(200)));
                result.ShouldBeEquivalentTo(false);
            });

            var writeResult = m_lock.ExecuteWrite(() =>
            {
                s           = "Acquired Write";
                bool result = SyncExecuteOnOtherThread(() => m_lock.ExecuteRead(() => { throw new Exception("Read was allowed!"); }, TimeSpan.FromMilliseconds(200)));
                result.ShouldBeEquivalentTo(false);
                result = SyncExecuteOnOtherThread(() => m_lock.ExecuteWrite(() => { throw new Exception("Write was allowed!"); }, TimeSpan.FromMilliseconds(200)));
                result.ShouldBeEquivalentTo(false);
                result = SyncExecuteOnOtherThread(() => m_lock.ExecuteUpgradableRead(() => { throw new Exception("Write was allowed!"); }, TimeSpan.FromMilliseconds(200)));
                result.ShouldBeEquivalentTo(false);
                return("Done");
            });

            var lockWriteResult = m_lock.ExecuteWrite(() =>
            {
                s           = "Acquired Write";
                bool result = SyncExecuteOnOtherThread(() => m_lock.ExecuteRead(() => { throw new Exception("Read was allowed!"); }, TimeSpan.FromMilliseconds(200)));
                result.ShouldBeEquivalentTo(false);
                result = SyncExecuteOnOtherThread(() => m_lock.ExecuteWrite(() => { throw new Exception("Write was allowed!"); }, TimeSpan.FromMilliseconds(200)));
                result.ShouldBeEquivalentTo(false);
                result = SyncExecuteOnOtherThread(() => m_lock.ExecuteUpgradableRead(() => { throw new Exception("Write was allowed!"); }, TimeSpan.FromMilliseconds(200)));
                result.ShouldBeEquivalentTo(false);
                return("Done");
            }, TimeSpan.FromSeconds(1));

            lockWriteResult.Result.ShouldBeEquivalentTo("Done");
            lockWriteResult.State.ShouldBeEquivalentTo(LockResultOutcome.Completed);
            writeResult.ShouldBeEquivalentTo("Done");
            s.ShouldBeEquivalentTo("Acquired Write");
        }