Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What is best way of iterating over Dictionary with multiple threads?

EDIT I'm limited to the .Net version 2.0 so I don't believe I can use the Task Parallel Library in this case.

I've got a dictionary of objects.

I need to iterate over all of them and perform an expensive, but embarrassingly parallelizable , calculation on each element.

Currently I'm using a single thread to iterate over the entire dictionary.

Approach 1

I've toyed with using a thread pool to split the calculations over multiple threads but that leads to the question of how to pass this to separate threads?

I currently convert the keys collection to an array and pass part of the array to separate threads so they can use the key to look up the value and perform the calculation.

Approach 2

Alternatively I could iterate over each key and dispatch a thread in a thread pool to each element.

The second approach is slower.

Is there a better alternative?

like image 370
chollida Avatar asked Sep 08 '11 21:09

chollida


2 Answers

You can use the Task Parallel Library:

Parallel.ForEach(dictionary, keyValuePair => {...});
like image 121
Mark Cidade Avatar answered Sep 20 '22 10:09

Mark Cidade


you can use this approach (.NET 4)

var elements = new ConcurrentDictionary<int, string>();

      Parallel.ForEach(elements, (element) =>
                {
                    // USE element the way you need it
                }
like image 27
Davide Piras Avatar answered Sep 21 '22 10:09

Davide Piras