且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

并行 foreach 循环 - 奇怪的行为

更新时间:2022-05-26 03:24:59

List 确实不是线程安全的,所以 cumsupDataP.Add(...)正在以不可预测的方式丢弃数据.

List<T> is indeed not thread safe, so cumsupDataP.Add(...) is dropping data in unpredictable ways.

将该行替换为:

ConcurrentBag<List<double>> cumsumDataP = new ConcurrentBag<List<double>>();

一切都会好起来的.请注意,ConcurrentBag无序,但这很好,因为无论如何您都无法预测线程的顺序;p

and it will all work. Note that ConcurrentBag<T> is unordered, but that is fine because you have no way of predicting the order from the threads anyway ;p