Constructs a thread-safe generic collection of key/value pairs using the lock provided.
Syntax
Example
Library/Library.Test/TestSynchronizedCollections.cs
C# | Copy Code |
---|
LockCounterFactory<SimpleReadWriteLocking> factory = new LockCounterFactory<SimpleReadWriteLocking>();
ILockStrategy lck = factory.Create();
SynchronizedList<int> list = new SynchronizedList<int>(lck);
Assert.IsTrue(ReferenceEquals(lck, list.Lock));
list.Add(42);
Assert.AreEqual(1, factory.TotalWriterCount);
Assert.AreEqual(0, factory.TotalReaderCount);
list[0] = 51;
Assert.AreEqual(2, factory.TotalWriterCount);
list.Insert(1, 52);
Assert.AreEqual(3, factory.TotalWriterCount);
list.RemoveAt(1);
Assert.AreEqual(4, factory.TotalWriterCount);
list.Remove(-1);
Assert.AreEqual(5, factory.TotalWriterCount);
Assert.AreEqual(51, list[0]);
Assert.AreEqual(1, factory.TotalReaderCount);
foreach (int i in list)
GC.KeepAlive(i);
Assert.AreEqual(2, factory.TotalReaderCount);
Assert.AreEqual(0, list.IndexOf(51));
Assert.AreEqual(3, factory.TotalReaderCount);
Assert.AreEqual(1, list.Count);
Assert.AreEqual(4, factory.TotalReaderCount); |
VB.NET | Copy Code |
---|
Dim factory As New LockCounterFactory(Of SimpleReadWriteLocking)()
Dim lck As ILockStrategy = factory.Create()
Dim list As New SynchronizedList(Of Integer)(lck)
Assert.IsTrue(ReferenceEquals(lck, list.Lock))
list.Add(42)
Assert.AreEqual(1, factory.TotalWriterCount)
Assert.AreEqual(0, factory.TotalReaderCount)
list(0) = 51
Assert.AreEqual(2, factory.TotalWriterCount)
list.Insert(1, 52)
Assert.AreEqual(3, factory.TotalWriterCount)
list.RemoveAt(1)
Assert.AreEqual(4, factory.TotalWriterCount)
list.Remove(-1)
Assert.AreEqual(5, factory.TotalWriterCount)
Assert.AreEqual(51, list(0))
Assert.AreEqual(1, factory.TotalReaderCount)
For Each i As Integer In list
GC.KeepAlive(i)
Next
Assert.AreEqual(2, factory.TotalReaderCount)
Assert.AreEqual(0, list.IndexOf(51))
Assert.AreEqual(3, factory.TotalReaderCount)
Assert.AreEqual(1, list.Count)
Assert.AreEqual(4, factory.TotalReaderCount) |
Requirements
Target Platforms: Windows XP, Windows Server 2003, Windows Vista, Windows Server 2008, Windows 7
See Also