Search code examples
c#.netparallel-processingtask-parallel-libraryparallel.foreach

Parallel.ForEach loop is not working "it skips some and double do others"


I have 2 methods that can do the work for me, one is serial and the other one is parallel. The reason for parallelization is because there are lots of iteration(about 100,000 or so) For some reason, the parallel one do skip or double doing some iterations, and I don't have any clue how to debug it.

The serial method

for(int i = somenum; i >= 0; i-- ){

    foreach (var nue in nuelist)
    {
        foreach (var path in nue.pathlist)
        {
            foreach (var conn in nue.connlist)
            {
                Func(conn,path); 
            }
        }
    }
}

The parallel method

for(int i = somenum; i >= 0; i-- ){

    Parallel.ForEach(nuelist,nue =>
    {
        Parallel.ForEach(nue.pathlist,path=>
        {
            Parallel.ForEach(nue.connlist, conn=>
            {
                Func(conn,path);
            });
        });
    });
}

Inside Path class

Nue firstnue;
public void Func(Conn conn,Path path)
{
    List<Conn> list = new(){conn};
    list.AddRange(path.list);
    _ = new Path(list); 
}
public Path(List<Conn>)
{
   //other things
   firstnue.pathlist.Add(this);
   /*
   firstnue is another nue that will be 
   in the next iteration of for loop
   */
}

They are both the same method except, of course, foreach and Parallel.ForEach loop.

the code is for the code in here (GitHub page)


Solution

  • Thanks to sedat-kapanoglu, I found the problem is really about thread safety. The solution was to change every List<T> to ConcurrentBag<T>.

    For everyone, like me, The solution of "parallel not working with collections" is to change from System.Collections.Generic to System.Collections.Concurrent