Defines the lock used to provide tree-level exclusive operations. This should be set at the time of construction, or not at all since operations depending on this (Clear, EnableCount, and UnloadCache) may behave poorly if operations that started prior to setting this value are still being processed. Out of the locks I've tested the ReaderWriterLocking implementation performs best here since it is a highly read-intensive lock. All public APIs that access tree content will aquire this lock as a reader except the tree exclusive operations. This also allows you, by way of aquiring a write lock, to gain exclusive access and perform mass updates, atomic enumeration, etc.
Syntax
Visual Basic (Declaration) | |
---|
Public ReadOnly Property CallLevelLock As ILockStrategy |
Example
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 |
Requirements
Target Platforms: Windows XP, Windows Server 2003, Windows Vista, Windows Server 2008, Windows 7
See Also