并行foreach循环 - 古怪的行为 [英] parallel foreach loop - odd behavior

查看:269
本文介绍了并行foreach循环 - 古怪的行为的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

下面简单的code产生的随机数表>,然后计算在一个平行的foreach循环每个列表的累计总和。为什么我会得到不到'numLists的评价?经常围绕9990我猜它是与线程安全。什么是另一种方法? (我是C#初学者,所以希望我使用正确的术语)谢谢。

 使用系统;
使用System.Collections.Generic;
使用System.Threading.Tasks;

命名空间testParallelForeach
{
    类节目
    {
        静态无效的主要(字串[] args)
        {

            名单<列表<双>> bsData =新的名单,其中,名单,其中,双>>();
            名单<列表<双>> cumsumDataP =新的名单,其中,名单,其中,双>>();
            INT numLists = 10000;
            INT myLen = 400;
            随机兰特=新的随机();
            的for(int i = 0; I< numLists;我++)
            {
                bsData.Add(新名单,其中,双>());
                对于(INT J = 0; J< myLen; J ++)
                {
                    bsData [I]。新增(rand.NextDouble());
                }
            }
            Parallel.ForEach(bsData,一个=> cumsumDataP.Add(CumulativeSumParallel(一)));
            Console.WriteLine(cumsumDataP.Count = {0},cumsumDataP.Count);
            Console.ReadKey();

        }

        公共静态列表<双> CumulativeSumParallel(名单<双> singleRetSeries)
        {
            INT R = singleRetSeries.Count;
            名单<双> cumsumList =新的名单,其中,双>();

            cumsumList.Add(singleRetSeries [0]);
            的for(int i = 1; I< R;我++)
            {
                cumsumList.Add(cumsumList [我 -  1] + singleRetSeries [I]);
            }
            返回cumsumList;
        }
    }
}
 

解决方案

名单,其中,T> 确实不是线程安全的,所以 cumsupDataP.Add (...)正在下降中未predictable方面的数据。

修改该行:

  ConcurrentBag<列表<双>> cumsumDataP =新ConcurrentBag<列表<双>>();
 

和它将所有的工作。需要注意的是 ConcurrentBag< T> 无序的,但是这是好的,因为你的的predicting没有办法的从线程反正秩序; P

The code below simply creates a List> of random numbers and then calculates the cumulative sum of each list in a parallel foreach loop. Why do I get less than 'numLists' evaluations? Often around 9990. I'm guessing it has something to do with thread safety. What's an alternative method? (I'm a C# beginner so hopefully I'm using correct terms) Thanks.

using System;  
using System.Collections.Generic;  
using System.Threading.Tasks;  

namespace testParallelForeach
{
    class Program
    {
        static void Main(string[] args)
        {

            List<List<double>> bsData = new List<List<double>>();
            List<List<double>> cumsumDataP = new List<List<double>>();
            int numLists = 10000;
            int myLen = 400;
            Random rand = new Random();
            for (int i = 0; i < numLists; i++)
            {
                bsData.Add(new List<double>());
                for (int j = 0; j < myLen; j++)
                {
                    bsData[i].Add(rand.NextDouble());
                }
            }
            Parallel.ForEach(bsData, a => cumsumDataP.Add(CumulativeSumParallel(a)));
            Console.WriteLine("cumsumDataP.Count={0}", cumsumDataP.Count);
            Console.ReadKey();

        }

        public static List<double> CumulativeSumParallel(List<double> singleRetSeries)
        {
            int r = singleRetSeries.Count;
            List<double> cumsumList = new List<double>();

            cumsumList.Add(singleRetSeries[0]);
            for (int i = 1; i < r; i++)
            {
                cumsumList.Add(cumsumList[i - 1] + singleRetSeries[i]);
            }
            return cumsumList;
        }
    }
}

解决方案

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

Replace that line with:

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

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

这篇关于并行foreach循环 - 古怪的行为的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆