Bug 18363 - Parallel.For uses unsafe 64 bit cross-thread accesses
Summary: Parallel.For uses unsafe 64 bit cross-thread accesses
Alias: None
Product: Class Libraries
Classification: Mono
Component: mscorlib ()
Version: unspecified
Hardware: PC Mac OS
: --- normal
Target Milestone: Untriaged
Assignee: Bugzilla
Depends on:
Reported: 2014-03-13 14:09 UTC by Zoltan Varga
Modified: 2015-03-30 10:05 UTC (History)
2 users (show)

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

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:

Description Zoltan Varga 2014-03-13 14:09:32 UTC
The Parallel.For implementation accesses the 'ranges' array in a non-safe manner, one thread accesses it using Interlocked.CompareExchange, while another accesses it directly:
range.V64.Actual = ++i;

This doesn't work on arm where the 64 bit Interlocked.CompareExchange operatives are only atomic in regards to each other, i.e. they are only atomic if every thread accesses the memory location using the Interlocked operations.

This causes failures in the corlib MonoTests.System.Threading.Tasks.ParallelTests.ParallelForTestCase test.

using System;
using System.Linq;
using System.Threading;
using System.Threading.Tasks;
using System.IO;
using System.Collections.Generic;
using System.Collections.Concurrent;

public class Tests
	public static void Main (String[] args) {
		for (int iter = 0; iter < 500; ++iter) {
			int[] actual = Enumerable.Range (1, 1000).ToArray ();
			SpinWait sw = new SpinWait ();

			Parallel.For (0, actual.Length, (i) => { if (actual [i] != i + 1) Console.WriteLine ("F: " + i + " " + actual [i]); actual[i] *= 2; sw.SpinOnce (); });
Comment 1 Marek Safar 2015-03-30 10:05:47 UTC
The test was reenabled after reference source import