2
\$\begingroup\$

i have this code that is suppose to work with at starting the minimum number of threads, say 5. when it starts the first five threads, it automatically replaces any thread that is finished making sure that there is always five threads working.

the problem here is that, am finding it difficult to manage where am suppose perform locks

Private Sub StartProcess()
 intNextItemIndex = intMaxThreadCount
 ' start the first five threads
 For i = 0 To intMaxThreadCount
 If i < ListWorkItems.Count Then
 StartNextItem(i)
 End If
 Next
 ' update running threads
 End Sub
 Private Sub StartNextItem(ByVal index As Integer)
 Dim tt As New ThreadTask
 If index < ListWorkItems.Count Then
 ' start parsing thread
 Dim t As New Thread(AddressOf DoWork)
 With tt
 .Index = index
 .Task = "count"
 .Data = CStr(index * index)
 End With
 With t
 .IsBackground = True
 .Name = "Thread " & index
 .Start(tt)
 End With
 Thread.Sleep(100)
 End If
 End Sub
 Private Sub DoWork(ByVal data As Object)
 Dim tt As ThreadTask = CType(data, ThreadTask)
 Dim sTemp As String = Nothing
 Dim objLock As Object = New Object()
 Dim t As Double = 0
 myWatch = New Stopwatch
 Try
 Debug.Print("Incoming index: " & CInt(tt.Index))
 myWatch.Start()
 SyncLock (objLock)
 sTemp = PerformWork(ListWorkItems(CInt(tt.Index)).ToString)
 'sTemp = CountTo()
 End SyncLock
 myWatch.Stop()
 t = myWatch.ElapsedMilliseconds / 1000
 AddItem(tt.Index, t.ToString & " = " & sTemp)
 'AddItem(index, t.ToString)
 Catch ex As Exception
 ' catch error
 Debug.Print( ex.Message)
 End Try
 end sub
Public Sub AddItem(ByVal index As Integer, ByVal item As String)
 If Me.InvokeRequired Then
 Me.Invoke(New AddItemDelegate(AddressOf AddItem), index, item)
 Else
 Dim objLock As Object = New Object()
 Dim lvItem As New ListViewItem
 SyncLock (objLock)
 count += 1
 intNextItemIndex += 1
 End SyncLock
 With lvItem
 .Text = index.ToString
 .SubItems.Add(ListWorkItems(index).ToString)
 If item.Length < 100 Then
 .SubItems.Add(item.ToString)
 Else
 .SubItems.Add(item.Length.ToString)
 End If
 End With
 listView1.Items.Add(lvItem)
 If count = ListWorkItems.Count Then
 Button1.Enabled = True
 tsslStatus.Text = "Finished!!"
 Debug.Print("Finished: " & index.ToString)
 Else
 tsslStatus.Text = "Processing item (" & count + 1 & ")"
 End If
 StartNextItem(intNextItemIndex)
 End If
End Sub

can someone help me review it and tell me whats wrong with it?

Are the loks ok?

Is this way of doing it ok?

What possible improvements can i make

thanks all

asked Jun 17, 2011 at 10:55
\$\endgroup\$

1 Answer 1

1
\$\begingroup\$

Unfortunately, the whole approach is broken. You should make your parallel tasks explicit in the code so that you don’t need cross-thread communication any more. Secondly, you are writing your own thread scheduler here. – Don’t. Schedulers already exist, take advantage of that.

Have a look at the Task Parallel Library, in particular the Task and TaskScheduler classes.

Better yet, try to model your algorithm to benefit from data parallelism and eschew explicit threading completely in favour of the automatically parallelised computations.

answered Jun 17, 2011 at 11:40
\$\endgroup\$
2
  • \$\begingroup\$ am using .net 2.0, of which the task Parallel is not available \$\endgroup\$ Commented Jun 17, 2011 at 11:44
  • \$\begingroup\$ @Smith That’s a problem. Incidentally, why are you using a completely outdated (> 5 years old, > 2 versions outdated) version of .NET? \$\endgroup\$ Commented Jun 17, 2011 at 11:45

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.