D
D
Daniel2018-08-22 15:07:42
C++ / C#
Daniel, 2018-08-22 15:07:42

How to keep a counter of successful operations using Parallel.ForEach?

There is a list of file paths. It is necessary to perform a certain action on each file and increase the counter if this action was completed successfully.
To speed up the processing of the list of files, it was decided to use a parallel loop. Processing the entire list began to take quite a bit of time, but the counter of successful operations began to give strange values.
For example, 250 files have been successfully processed, and the counter shows 510. Blocking the increment of a variable with the lock statement does not help, and it is extremely necessary to get the exact number of successful operations. Tell me, please, how can I accurately calculate the number of operations performed using the Parallel.ForEach parallel loop?
Below is the prototype code for the required task:

IEnumerable<string> files = new IEnumerable<string>();
files = Directory.EnumerateFiles(@"C:\test"); // +1300 путей к файлам
int successCounter = 0;
object _lock = new object();
Parallel.ForEach(files, f =>
{
if(Operation(f))
{
lock(_lock)
successCounter++;
}
});
Console.WriteLine($"Обработано: {successCounter.ToString()} из {files.Count().ToString()} файлов.");

successCounter in this case can be: 2697, 2606, 2631. But never near the true number of files - 1300. It feels like: either there are 2 times more parallel iterations than necessary, or one iteration increments an arbitrary value to the counter.
Help, please, deal with this problem.

Answer the question

In order to leave comments, you need to log in

1 answer(s)
P
Peter, 2018-08-22
@Dyaminigo

private volatile int counter;
Interlocked.Increment(ref counter);

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question