BPlusTree/BPlusTree.Test/ThreadedBTreeTest.cs
C# | Copy Code |
---|---|
BPlusTree<int, int>.OptionsV2 options = new BPlusTree<int, int>.OptionsV2( new PrimitiveSerializer(), new PrimitiveSerializer()); options.LockTimeout = 100; options.CallLevelLock = new ReaderWriterLocking(); using (BPlusTree<int, int> dictionary = new BPlusTree<int, int>(options)) { bool canwrite = false, canread = false; ThreadStart proc = delegate() { try { dictionary[1] = 1; canwrite = true; } catch { canwrite = false; } try { int i; dictionary.TryGetValue(1, out i); canread = true; } catch { canread = false; } }; Assert.IsTrue(proc.BeginInvoke(null, null).AsyncWaitHandle.WaitOne(1000, false)); Assert.IsTrue(canwrite); Assert.IsTrue(canread); //now we lock the entire btree: using (dictionary.CallLevelLock.Write()) { //they can't read or write Assert.IsTrue(proc.BeginInvoke(null, null).AsyncWaitHandle.WaitOne(1000, false)); Assert.IsFalse(canwrite); Assert.IsFalse(canread); //but we can proc(); Assert.IsTrue(canwrite); Assert.IsTrue(canread); } //lock release all is well Assert.IsTrue(proc.BeginInvoke(null, null).AsyncWaitHandle.WaitOne(1000, false)); Assert.IsTrue(canwrite); Assert.IsTrue(canread); //We can also make sure noone else gains exclusive access with a read lock using (dictionary.CallLevelLock.Read()) { Assert.IsTrue(proc.BeginInvoke(null, null).AsyncWaitHandle.WaitOne(1000, false)); Assert.IsTrue(canwrite); Assert.IsTrue(canread); } } |
VB.NET | Copy Code |
---|---|
Dim options As New BPlusTree(Of Integer, Integer).OptionsV2(New PrimitiveSerializer(), New PrimitiveSerializer()) options.LockTimeout = 100 options.CallLevelLock = New ReaderWriterLocking() Using dictionary As New BPlusTree(Of Integer, Integer)(options) Dim canwrite As Boolean = False, canread As Boolean = False Dim proc As ThreadStart = Function() Do Try dictionary(1) = 1 canwrite = True Catch canwrite = False End Try Try Dim i As Integer dictionary.TryGetValue(1, i) canread = True Catch canread = False End Try End Function Assert.IsTrue(proc.BeginInvoke(Nothing, Nothing).AsyncWaitHandle.WaitOne(1000, False)) Assert.IsTrue(canwrite) Assert.IsTrue(canread) 'now we lock the entire btree: Using dictionary.CallLevelLock.Write() 'they can't read or write Assert.IsTrue(proc.BeginInvoke(Nothing, Nothing).AsyncWaitHandle.WaitOne(1000, False)) Assert.IsFalse(canwrite) Assert.IsFalse(canread) 'but we can proc() Assert.IsTrue(canwrite) Assert.IsTrue(canread) End Using 'lock release all is well Assert.IsTrue(proc.BeginInvoke(Nothing, Nothing).AsyncWaitHandle.WaitOne(1000, False)) Assert.IsTrue(canwrite) Assert.IsTrue(canread) 'We can also make sure noone else gains exclusive access with a read lock Using dictionary.CallLevelLock.Read() Assert.IsTrue(proc.BeginInvoke(Nothing, Nothing).AsyncWaitHandle.WaitOne(1000, False)) Assert.IsTrue(canwrite) Assert.IsTrue(canread) End Using End Using |
System.Object
CSharpTest.Net.Serialization.PrimitiveSerializer
Target Platforms: Windows XP, Windows Server 2003, Windows Vista, Windows Server 2008, Windows 7