我在嵌套的 Parallel.For 循环 (vb.net) 中调用了一个子例程 MyPartsMatrix。MyPartsMatrix 需要一个名为“未填充”的变量,该变量通过 ByRef 传递,因为该值在 MyPartsMatrix 子例程中被修改。在子例程 MyPartsMatrix 执行后,我需要获取并存储此值。
与使用普通嵌套 For...Next 循环的非并行代码相比,当我运行此代码的并行版本时,“未填充”变量会产生不同的值。我不明白为什么会这样。
从 Parallel.For 循环内部调用另一个子例程是否线程安全?
这个变量“未填充”线程安全吗?
Dim ConcurrentListofResults As ConcurrentQueue(Of FindBestResults)
ConcurrentListofResults = New ConcurrentQueue(Of FindBestResults)
Dim x = 5, y = 5
Parallel.For(0, x, Sub(oD)
Parallel.For(0, y, Sub(oT)
Dim unfilled As Integer = 0
MyPartsMatrix (oD, oT, unfilled)
'Create a FBS item to add to the concurrent list collection
Dim FBSResultsItem = New FindBestResults
FBSResultsItem.oD = oD
FBSResultsItem.oT = oT
FBSResultsItem.unfilled = unfilled
'Add this item to the Concurent collection
ConcurrentListofResults.Enqueue(FBSResultsItem)
End Sub)
End Sub)
'Get best result.
Dim bestResult As FindBestResults
For Each item As FindBestResults In ConcurrentListofResults
If item.unfilled < bestResult.unfilled Then
bestResult.oD = item.oD
bestResult.oT = item.oT
bestResult.unfilled = item.unfilled
End If
Next
Public Sub MyPartsMatrix (ByVal oD As Integer, ByVal oT As Integer, ByRef unfilled As Integer)
'....do stuff with the unfilled variable....
'unfilled is a counter that is incremented while we run through the PartsMatrix
unfilled = unfilled + 1
End Sub
如果这不是线程安全的,是否有另一种方法来编写它,以便“未填充”变量是线程安全的,或者使调用另一个子例程线程安全?