[RESOLVED] [2005] Threading and Synchronization
Top of the morning to you all!
I am just having a little trouble understanding thread synchronization. Allow me to demonstrate.
I have a class that exposes 3 methods like so:
Code:
Class a
Private x As Collection
Public Sub Add()
End Sub
Public Sub Remove()
End Sub
Public Sub Print()
For counter As Integer = 0 To x.Count-1 Step 1
Next counter
End Sub
End Class
Now I have declared an instance of 'class a' and multiple threads are able to perform actions upon it.
My problem comes when one thread is running a print operation and another may be removing an item from the collection. The print operation is printing the data in a loop, when the loop began 'counter' was set to the count of the collection. The collection's size has since changed and we are headed for an "index out of bounds" error.
I need to lock the instance of the class as a whole so that only one thread can perform operations on the instance at any one time.
I know about the 'SyncLock' feature but am unsure if this is sufficient. Is the answer just to synclock the calls to the class methods? E.G...
Code:
Dim objSync as Object
SyncLock objSync
instance.Print()
End SyncLock
Any pointers or leads would be greatly appreciated,
Have a great morning!
Matt.
Re: [2005] Threading and Synchronization
It would be more fitting to handle the synchronisation within ClassA, e.g.:
vb Code:
Public Class ClassA
Private items As ArrayList
Public Sub Add()
SyncLock items.SyncRoot
'Add item here.
End SyncLock
End Sub
Public Sub Remove()
SyncLock items.SyncRoot
'Remove item here.
End SyncLock
End Sub
Public Sub Print()
SyncLock items.SyncRoot
For index As Integer = 0 To Me.items.Count - 1 Step 1
Next index
End SyncLock
End Sub
End Class
You now have three linked critical sections. Only one thread can enter any one of the critical sections at a time. If a thread enters the Print method and enters the SyncLock block, then another thread enters the Add or Remove method, the second thread will have to wait at the SyncLock statement until the first thread completes its job and exits the SyncLock block in the Print method. It's like having a house with multiple doors and rooms and a sensor that can tell when someone is inside. It doesn't matter which door someone enters or which room they're in, as long as someone is inside all entrances remain locked until they exit.
Re: [2005] Threading and Synchronization
PERFECT! Absolutely <expletive> PERFECT!!!
Damn JM your onto it mate! That is EXACTLY what I was looking for.
Using that theory, I could declare a member variable of type Object, instantiate it in the constructor and lock my critical sections using the same object each time? By using the same object instance I will create the desired effect, basically copy the SyncRoot feature. Yeah?
I was not properly conceptualizing the function of the lock object in the SyncLock statement...all clear now though.
Thanks AGAIN JM, you always seem to sort us out!
Matt.
Re: [RESOLVED] [2005] Threading and Synchronization
Are you trying to create your own strongly-typed collection or just a class that contains a collection?