我有一个具有许多属性的类,它们是Integer
和Single
。我想以多线程方式使用该类,以便可以将属性用作累加器(该类是报告方案的基础)。所以我希望能够做这样的事情:
Public Class ReportTotals
Property Count As Integer
Property Total As Single
Property Tax As Single
Property Shipping As Single
Property ItemsSold As Integer
Public Function GetReport() As String
...
End Function
End Class
Public Function BuildReportData As ReportTotals
Dim myReport As New ReportTotals
With myReport
Parallel.ForEach(UserSales, Sub(userSale)
.Count += 1
.Total += userSale.Total
.Tax += userSale.Tax
.Shipping += userSale.Shipping
.ItemsSold += userSale.ItemsSold
'more complicated stuff and property assignments
End Sub)
End With
End Function
根据我的研究,我知道这一点Integer
并且Single
是原子的,但我不确定这是否扩展到作为类的一部分的整数。我不想假设,因为多线程错误可能会在以后出现并咬我。
更新:显然,Single
不是线程安全的,所以我必须在那个上使用锁,但是呢Integer
?