Click here to Skip to main content
15,887,945 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Can anyone show me an example of taking a list, doing some operation in each element in the list's property using a parallel for loop and then returning the original list? For example
C#
List<t> someList = new List<t>
for(int i= 0; i < originallist.count;i++)
{
    Do operation on originallist[i].somevariable
    someList.add(originallist[i])
}
return someList


In other words. HOW would I parallelize this???
Posted
Updated 4-Dec-12 18:45pm
v2

I suggest going through this article Task Parallel Library: 3 of n[^]. Also note that the whole serie is a really good read.
 
Share this answer
 
v2
Comments
Espen Harlinn 11-Dec-12 17:50pm    
5'ed!
Wendelius 12-Dec-12 0:17am    
Thanks :)
Hi,

Please refer to the following document: How to: Write a Simple Parallel.For Loop[^]

Here is a code snippet:

C#
static void MultiplyMatricesParallel(double[,] matA, double[,] matB, double[,] result)
{
    int matACols = matA.GetLength(1);
    int matBCols = matB.GetLength(1);
    int matARows = matA.GetLength(0);

    // A basic matrix multiplication.
    // Parallelize the outer loop to partition the source array by rows.
    Parallel.For(0, matARows, i =&gt;
    {
        for (int j = 0; j &lt; matBCols; j++)
        {
            // Use a temporary to improve parallel performance.
            double temp = 0;
            for (int k = 0; k &lt; matACols; k++)
            {
                temp += matA[i, k] * matB[k, j];
            }
            result[i, j] = temp;
        }
    }); // Parallel.For
}


Kind regards,
 
Share this answer
 
Comments
Espen Harlinn 11-Dec-12 17:50pm    
5'ed!

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900