0

Can someone help me understand how synclock statements work? I know there are some other posts about synclock here but I was not able to follow the answers very well. I am trying to get a simple multithreading example up and running but am having trouble. At first I tried the following code.

Public Class Class1
    Public list As List(Of Integer) = New List(Of Integer)

    Public Sub addInt(ByVal i As Integer)
        SyncLock list
            list.Add(i)
        End SyncLock
    End Sub

    Public Sub MainLoop()
        While list.Count < 50
            SyncLock list
                For Each i As Integer In list
                    Debug.WriteLine(i.ToString())
                Next
            End SyncLock
        End While
    End Sub
End Class

I had a simple winform with two buttons. I used the first button to create a obj of Class1 and start the MainLoop method in a new thread. And I used the seconded button to call the addInt method. However the code might work for a press or two and then lock up. After reading the other questions from this form I realized that the lockobject for the synclock statement did not work as I initially thought, and was just an arbitrary reference object that should not ever be changed. I think the syncLock statement just forces the whole code block to execute before passing processor control to a new thread. So I tried this using another lock object but now it just locks up.

Public Class Class1
    Private padLock As String = "PADLOCK"
    Public list As List(Of Integer) = New List(Of Integer)

    Public Sub addInt(ByVal i As Integer)
        SyncLock padLock
            list.Add(i)
        End SyncLock

    End Sub

    Public Sub MainLoop()
        While list.Count < 50
            SyncLock padLock
                For Each i As Integer In list
                    Debug.WriteLine(i.ToString())
                Next
            End SyncLock
        End While
    End Sub
End Class

Can someone explain why the above code does not work? In a similar post someone mentioned that the Interlocked class would be useful but I could not figure out what that is or how that is used. A brief "Interlocked Class For Dummies" explanation would also be appreciated.

LarsTech
  • 80,625
  • 14
  • 153
  • 225
Alexander Van Atta
  • 870
  • 11
  • 34
  • It could just be doing the thing forever if `Count` never changes, essentially _looking_ for all practical purposes that is has indeed locked up. And I don't see that `addInt` is used anywhere. – Grant Thomas Sep 26 '12 at 14:37
  • AddInt is used when I press the second button on the winform. See the paragraph after the first block of code for more info. – Alexander Van Atta Sep 26 '12 at 14:43
  • Interlocked will *not* be helpful here. There's no obvious reason why this code would deadlock, you need to post a better example. – Hans Passant Sep 26 '12 at 15:31
  • This code looks fine. It's possible that the problem is in the code that calls this. Would you be able to reduce the code down to the smallest complete example that reproduces the problem and post all of that code together? – Steven Doggart Sep 26 '12 at 15:41
  • 1
    Don't lock on a string literal (very very dangerous just like locking on a type). Lock on a `new object()`. – usr Sep 26 '12 at 17:23
  • Ok Is it ok to lock on a Object marked as Friend instead of Private so that I can lock code in another class in the same assembly? – Alexander Van Atta Sep 26 '12 at 17:55

1 Answers1

0

Ok I think I figured out the problem. I don't think my thread was deadlocking I think it was just starving my Application.Run() Thread of resources. When I added a Thread.Sleep(500) call after the End SyncLock in the MainLoop method the program worked as expected.

Alexander Van Atta
  • 870
  • 11
  • 34