Results 1 to 4 of 4

Thread: [RESOLVED] [2005] Threading and Synchronization

  1. #1

    Thread Starter
    Lively Member
    Join Date
    Mar 2006
    Posts
    65

    [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.
    Last edited by GottaGetITDone; Mar 11th, 2007 at 10:26 PM. Reason: Resolved by the weapon JM!

  2. #2
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,221

    Re: [2005] Threading and Synchronization

    It would be more fitting to handle the synchronisation within ClassA, e.g.:
    vb Code:
    1. Public Class ClassA
    2.  
    3.     Private items As ArrayList
    4.  
    5.     Public Sub Add()
    6.         SyncLock items.SyncRoot
    7.             'Add item here.
    8.         End SyncLock
    9.     End Sub
    10.  
    11.     Public Sub Remove()
    12.         SyncLock items.SyncRoot
    13.             'Remove item here.
    14.         End SyncLock
    15.     End Sub
    16.  
    17.     Public Sub Print()
    18.         SyncLock items.SyncRoot
    19.             For index As Integer = 0 To Me.items.Count - 1 Step 1
    20.  
    21.             Next index
    22.         End SyncLock
    23.     End Sub
    24.  
    25. 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.
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

  3. #3

    Thread Starter
    Lively Member
    Join Date
    Mar 2006
    Posts
    65

    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.

  4. #4
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,221

    Re: [RESOLVED] [2005] Threading and Synchronization

    Are you trying to create your own strongly-typed collection or just a class that contains a collection?
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width