Bug 25583 - Processing lacks when I use Parallel.ForEach in iOS
Summary: Processing lacks when I use Parallel.ForEach in iOS
Status: VERIFIED ANSWERED
Alias: None
Product: iOS
Classification: Xamarin
Component: Xamarin.iOS.dll ()
Version: XI 8.4.0
Hardware: Macintosh Mac OS
: Normal normal
Target Milestone: Untriaged
Assignee: Bugzilla
URL:
Depends on:
Blocks:
 
Reported: 2014-12-22 23:56 UTC by sin
Modified: 2014-12-29 08:53 UTC (History)
3 users (show)

Tags:
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 Developer Community or GitHub 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:
VERIFIED ANSWERED

Description sin 2014-12-22 23:56:58 UTC
iOS

**********************************************
//test code
**********************************************
public override void ViewDidLoad() {
    base.ViewDidLoad();

    var button = UIButton.FromType(UIButtonType.RoundedRect);
    button.Frame = new RectangleF(0, 20, (float)View.Frame.Width, 40);
    button.SetTitle("Start", UIControlState.Normal);
    View.AddSubview(button);

    var textView = new UITextView(new CGRect(0, 60, (float)View.Frame.Width, (float)View.Frame.Height - 60));
    View.AddSubview(textView);

    button.TouchUpInside += (sender, args) => {
        var collection = new[] { 10, 20, 30, 40, 50, 60, 70, 80, 90, 100 };
        var sb = new StringBuilder();

        var stopwatch = System.Diagnostics.Stopwatch.StartNew();
        foreach (var item in collection) {
            sb.Append(item + "\r\n");
            Thread.Sleep(1000);
        }
        stopwatch.Stop();
        sb.Append(stopwatch.ElapsedMilliseconds.ToString("Normal : 0[ms]\r\n"));

        stopwatch.Restart();
        System.Threading.Tasks.Parallel.ForEach(collection, item => {
            sb.Append(item + "\r\n");
            Thread.Sleep(1000);
        });
        stopwatch.Stop();
        sb.Append(stopwatch.ElapsedMilliseconds.ToString("Parallel : 0[ms]\r\n"));

        textView.Text = sb.ToString();
    };
}

//implementation results by iPhone6

**********************************************
// first test 90 not found
**********************************************
10
20
30
40
50
60
70
80
90
100
Normal : 10015{ms}
50
100
40
30
80
70
20
60
10
Parallel : 5094{ms}


**********************************************
// second test 40 and 60 not found 
**********************************************
10
20
30
40
50
60
70
80
90
100
Normal : 10015{ms}
50
100
90
80
30
70
20
10
Parallel : 5089{ms}
Comment 1 Ram Chandra 2014-12-23 10:03:04 UTC
I have checked this issue and I am able to reproduce this behavior.

When I run the test case mentioned in bug description I observed that Parallel.ForEach is missing processing of items.  

Screencast: http://www.screencast.com/t/w787YIijI1mC

Environment Info:

=== Xamarin Studio ===

Version 5.5.4 (build 15)
Installation UUID: 6ea47b0d-1852-4aaf-808d-373ff0a5002b
Runtime:
	Mono 3.10.0 ((detached/92c4884)
	GTK+ 2.24.23 (Raleigh theme)

	Package version: 310000031

=== Xamarin.Android ===

Version: 4.20.0.28 (Trial Edition)
Android SDK: /Users/jatin66/Desktop/Backup/android-sdk-macosx
	Supported Android versions:
		1.6    (API level 4)
		2.1    (API level 7)
		2.2    (API level 8)
		2.3    (API level 10)
		3.0    (API level 11)
		3.1    (API level 12)
		3.2    (API level 13)
		4.0    (API level 14)
		4.0.3  (API level 15)
		4.1    (API level 16)
		4.2    (API level 17)
		4.3    (API level 18)
		4.4    (API level 19)
		4.4.87 (API level 20)
		5.0    (API level 21)
Java SDK: /usr
java version "1.7.0_65"
Java(TM) SE Runtime Environment (build 1.7.0_65-b17)
Java HotSpot(TM) 64-Bit Server VM (build 24.65-b04, mixed mode)

=== Apple Developer Tools ===

Xcode 6.1 (6604)
Build 6A1052d

=== Xamarin.iOS ===

Version: 8.4.0.47 (Trial Edition)
Hash: 7244769
Branch: 
Build date: 2014-12-11 14:54:30-0500

=== Xamarin.Mac ===

Version: 1.10.0.18 (Trial Edition)

=== Build Information ===

Release ID: 505040015
Git revision: f93940a35458a18052f1a25e106e62ca970d9c40
Build date: 2014-11-19 15:32:41-05
Xamarin addins: dc23cbd91a3a0e1d326328e1229e86c942a49ec8

=== Operating System ===

Mac OS X 10.10.0
Darwin Jatin66s-iMac.local 14.0.0 Darwin Kernel Version 14.0.0
    Fri Sep 19 00:26:44 PDT 2014
    root:xnu-2782.1.97~2/RELEASE_X86_64 x86_64
Comment 2 sin 2014-12-23 11:20:32 UTC
A problem did not occur when I changed StringBuilder in NSMutableString
Will StringBuilder become thread safe?
Comment 3 Rolf Bjarne Kvinge [MSFT] 2014-12-23 12:26:41 UTC
The problem is that you're adding strings to StringBuilder from multiple threads at the same time, this will result in undefined behavior.

Locking the StringBuilder will make this work as expected:

    lock (sb)
         sb.Append(item + "\r\n");

Note that neither StringBuilder nor NSMutableString are thread-safe.
Comment 4 sin 2014-12-23 13:04:43 UTC
I confirmed that I was settled in lock.
It was my mistake.
Thank you.
Comment 5 Ram Chandra 2014-12-29 08:53:06 UTC
As per comment 4 this issue is working fine. Hence, I am closing this issue.