Bug 24891 - Parallel.ForEach overload always sets -1 as current element index
Summary: Parallel.ForEach overload always sets -1 as current element index
Status: RESOLVED FIXED
Alias: None
Product: Class Libraries
Classification: Mono
Component: General ()
Version: unspecified
Hardware: PC Linux
: --- normal
Target Milestone: Untriaged
Assignee: Bugzilla
URL:
Depends on:
Blocks:
 
Reported: 2014-11-27 09:06 UTC by Jure Špik
Modified: 2014-11-27 12:22 UTC (History)
3 users (show)

Tags:
Is this bug a regression?: ---
Last known good build:


Attachments
console app to demonstrate wrong index in Parallel.ForEach (337 bytes, text/plain)
2014-11-27 09:06 UTC, Jure Špik
Details


Notice (2018-05-24): bugzilla.xamarin.com is now in read-only mode.

Please join us on Visual Studio Developer Community and in the Xamarin and Mono organizations on GitHub to continue tracking issues. Bugzilla will remain available for reference in read-only mode. We will continue to work on open Bugzilla bugs, copy them to the new locations as needed for follow-up, and add the new items under Related Links.

Our sincere thanks to everyone who has contributed on this bug tracker over the years. Thanks also for your understanding as we make these adjustments and improvements for the future.


Please create a new report on GitHub or Developer Community with your current version information, steps to reproduce, and relevant error messages or log files if you are hitting an issue that looks similar to this resolved bug and you do not yet see a matching new report.

Related Links:
Status:
RESOLVED FIXED

Description Jure Špik 2014-11-27 09:06:56 UTC
Created attachment 8892 [details]
console app to demonstrate wrong index in Parallel.ForEach

Using Parallel.ForEach overload ForEach<TSource>(IEnumerable<TSource>, Action<TSource, ParallelLoopState, Int64>)
should set the current item index for use in the delegate.

In Mono, the index is always -1.

I have tried it on mono 3.2.8 and 3.99.0 (master/3080be5 Thu Nov 27 14:30:18 CET 2014)

I have attached a demo console app that demonstrates the issue:
Windows output (numbers are "random"):
#0
#9
#2
#7
#5
#8
#4
#6
#1

Mono output:
#-1
#-1
#-1
#-1
#-1
#-1
#-1
#-1
#-1
#-1

I think the problem is in mcs/class/corlib/System.Threading.Tasks/Parallel.cs but I don't know how to fix it.

Also, other overloads of ForEach which return the current element index might be affected but I haven't tried them.
Comment 2 Jure Špik 2014-11-27 09:55:34 UTC
akoeplinger on #mono discovered that changing line 451 in Parallel.cs seems to fix the issue:
original line:
 (e, s, l) => { body (e, s, -1); return null; },

akoeplinger> changing it to (e, s, i, l) => { body (e, s, i); ... seems to have worked
Comment 3 Alexander Köplinger 2014-11-27 10:16:12 UTC
Fixed in master 0602561a31493ec9f64ad21c5719668a5fe2f445.