Synclock VB.NET

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • Gangreen
    New Member
    • Feb 2008
    • 98

    Synclock VB.NET

    there is a shared datastructure I'm using, and I want to make sure no 2 threads are altering it at the same time.

    I have something like:

    Sub1
    Synclock Locker
    do something with shared datastructure.
    if (some condition) then sub 2
    end synclock
    end Sub


    Sub2
    Synclock Locker
    do something with shared datastructure.
    end Synclock
    end Sub

    My question is: would this impose a problem? because when sub 1 is being executed within the synclock, it could call sub 2, which also uses the synclock.
  • Plater
    Recognized Expert Expert
    • Apr 2007
    • 7872

    #2
    Yes, that would cause deadlocks.
    You should rethink your code structure and try to implement them so that they don't need to call each other, or change where the locks/unlocks occur.

    Comment

    • Gangreen
      New Member
      • Feb 2008
      • 98

      #3
      ok, thank you!

      I'll figure something out.

      Comment

      • Gangreen
        New Member
        • Feb 2008
        • 98

        #4
        let's say I do this:

        Sub1
        Synclock Locker
        do something with shared datastructure.
        if (some condition) then sub 2
        end synclock
        end Sub


        Sub2
        do something with shared datastructure.
        end Sub

        would the code in sub 2 be executed within the locker? as in, can no other thread be altering the datastructure then?

        Comment

        • Plater
          Recognized Expert Expert
          • Apr 2007
          • 7872

          #5
          As long as Sub1 waits for sub2 to finish before continuing on, then the locker would still be locked.

          Comment

          Working...