Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

Another thread question

Posted on 2011-03-10
3
Medium Priority
?
210 Views
Last Modified: 2012-05-11
My question is around locking objects. I'm not clear when it is necessary and when it isn't in my particular case.

I have a loop that loops through a lst of vendors (see code below).

I also have a eventhandler that tells me when the thread is finished. I update the vendors last update dates and set it to CanStart = true.

My question is do I need to use the lock in the for loop where I'm starting the threads as well as what I'm doing in the event handler?

I just don't want to run into a locked condition.


For Each v In lstVendors
                If blnStopAllThreads = False AndAlso v.CanStart = True AndAlso _
                   UpdateQueue.Count > 0 AndAlso intUpdateThreadCnt < MaxUpdateThreads Then
                    v.CanStart = False
                    'Create a new thread
                    intUpdateThreadCnt += 1
                    Dim UThread As New UpdateThread("UpdateThread - " & CStr(intUpdateThreadCnt))
                    UThread.MyVendor = v
                    AddHandler UThread.ThreadCompleted, AddressOf UpdateThreadCompletedEventHandler
                    AddHandler UThread.ThreadVendorUpdated, AddressOf UpdateThreadVendorLastUpdateChangedEventHandler
                    Dim MyThread As Thread = New Thread(AddressOf UThread.UpdateData)
                    MyThread.Priority = ThreadPriority.Normal
                    MyThread.Name = "UpdateThread - " & CStr(intUpdateThreadCnt)
                    lstUpdateThreads.Add(MyThread)
                    MyThread.Start()
                End If

            Next








 Private Sub UpdateThreadVendorLastUpdateChangedEventHandler(ByVal v As Vendor)
        Dim i As Integer
        'LogOps.LogMessage("INFO", "ThreadVendorLastUpdateChangedEventHandler-ThreadCompletedEvent", "In Update Changed Event Handler")
        Try
            boolMonitorEntered = Monitor.TryEnter(MySyncLock, 15000)
            boolLockCreated = True

            If boolMonitorEntered Then
                For i = 0 To lstVendors.Count - 1
                    If lstVendors.Item(i).ForeignSysExchangeId = v.ForeignSysExchangeId Then
                        lstVendors.Item(i).LastAllergyUpdateDate = v.LastAllergyUpdateDate
                        lstVendors.Item(i).LastMedUpdateDate = v.LastMedUpdateDate
                        lstVendors.Item(i).LastRxUpdateDate = v.LastRxUpdateDate
                        lstVendors.Item(i).CanStart = True
                        Exit For
                    End If
                Next
            End If
        Finally
            If boolLockCreated = True Then
                Monitor.Exit(MySyncLock)
            End If
        End Try
    End Sub

Open in new window

0
Comment
Question by:rutledgj
  • 2
3 Comments
 
LVL 86

Expert Comment

by:Mike Tomlinson
ID: 35102274
Whether the lock is necessary or not depends on if more than one thread can ever satisfy this condition in your update handler at the same time:

    If lstVendors.Item(i).ForeignSysExchangeId = v.ForeignSysExchangeId Then

If more than one thread can end up matching the same "record" then you do need the lock if you wish to prevent them both writing to the same record and possibly only doing a partial update as they step on each other.

If every single thread will always be updating a unique record then the lock is not necessary.

One thread for every record is probably not a good approach though...  =\
How many are we talking about here?

A ThreadPool might be a better design.
0
 

Author Comment

by:rutledgj
ID: 35102615
Each thread operates on a single unique vendor which can contain any number of customers. My question isn't about having a lock in the eventhandler but in the for loop where I'm assigning the vendors to the threads.
0
 
LVL 86

Accepted Solution

by:
Mike Tomlinson earned 2000 total points
ID: 35102717
Gotcha...no, you should not need a lock up there.

A different issue, though, is that you are using a For Each iterator in the starter and possibly modifying that collection in the update handler.  If the update handler is fired before all the threads are started it could cause an issue.

Using an indexed for loop in both places would probably fix that if the issue arises.
0

Featured Post

Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Article by: jpaulino
XML Literals are a great way to handle XML files and the community doesn’t use it as much as it should.  An XML Literal is like a String (http://msdn.microsoft.com/en-us/library/system.string.aspx) Literal, only instead of starting and ending with w…
If you need to start windows update installation remotely or as a scheduled task you will find this very helpful.
Want to learn how to record your desktop screen without having to use an outside camera. Click on this video and learn how to use the cool google extension called "Screencastify"! Step 1: Open a new google tab Step 2: Go to the left hand upper corn…
Look below the covers at a subform control , and the form that is inside it. Explore properties and see how easy it is to aggregate, get statistics, and synchronize results for your data. A Microsoft Access subform is used to show relevant calcul…
Suggested Courses
Course of the Month6 days, 7 hours left to enroll

782 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question