Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8643759
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 12, 20262026-06-12T12:07:51+00:00 2026-06-12T12:07:51+00:00

I’m investigating the Parallelism Break in a For loop. After reading this and this

  • 0

I’m investigating the Parallelism Break in a For loop.

After reading this and this I still have a question:

I’d expect this code :

 Parallel.For(0, 10, (i,state) =>  
     { 
                Console.WriteLine(i); if (i == 5) state.Break(); 
     }

To yield at most 6 numbers (0..6).
not only he is not doing it but have different result length :

02351486
013542
0135642

Very annoying. (where the hell is Break() {after 5} here ??)

So I looked at msdn

Break may be used to communicate to the loop that no other iterations after the current iteration need be run.
If Break is called from the 100th iteration of a for loop iterating in
parallel from 0 to 1000, all iterations less than 100 should still be
run, but the iterations from 101 through to 1000 are not necessary.

Quesion #1 :

Which iterations ? the overall iteration counter ? or per thread ? I’m pretty sure it is per thread. please approve.

Question #2 :

Lets assume we are using Parallel + range partition (due to no cpu cost change between elements) so it divides the data among threads . So if we have 4 cores (and perfect divisions among them):

core #1 got 0..250
core #2 got 251..500
core #3 got 501..750
core #4 got 751..1000

so the thread in core #1 will meet value=100 sometime and will break.
this will be his iteration number 100 .
But the thread in core #4 got more quanta and he is on 900 now. he is way beyond his 100'th iteration.
He doesnt have index less 100 to be stopped !! – so he will show them all.

Am I right ? is that is the reason why I get more than 5 elements in my example ?

Question #3 :

How cn I truly break when (i == 5) ?

p.s.

I mean , come on ! when I do Break() , I want things the loop to stop.
excactly as I do in regular For loop.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-12T12:07:52+00:00Added an answer on June 12, 2026 at 12:07 pm

    To yield at most 6 numbers (0..6).

    The problem is that this won’t yield at most 6 numbers.

    What happens is, when you hit a loop with an index of 5, you send the “break” request. Break() will cause the loop to no longer process any values >5, but process all values <5.

    However, any values greater than 5 which were already started will still get processed. Since the various indices are running in parallel, they’re no longer ordered, so you get various runs where some values >5 (such as 8 in your example) are still being executed.

    Which iterations ? the overall iteration counter ? or per thread ? I’m pretty sure it is per thread. please approve.

    This is the index being passed into Parallel.For. Break() won’t prevent items from being processed, but provides a guarantee that all items up to 100 get processed, but items above 100 may or may not get processed.

    Am I right ? is that is the reason why I get more than 5 elements in my example ?

    Yes. If you use a partitioner like you’ve shown, as soon as you call Break(), items beyond the one where you break will no longer get scheduled. However, items (which is the entire partition) already scheduled will get processed fully. In your example, this means you’re likely to always process all 1000 items.

    How can I truly break when (i == 5) ?

    You are – but when you run in Parallel, things change. What is the actual goal here? If you only want to process the first 6 items (0-5), you should restrict the items before you loop through them via a LINQ query or similar. You can then process the 6 items in Parallel.For or Parallel.ForEach without a Break() and without worry.

    I mean , come on ! when I do Break() , I want things the loop to stop. excactly as I do in regular For loop.

    You should use Stop() instead of Break() if you want things to stop as quickly as possible. This will not prevent items already running from stopping, but will no longer schedule any items (including ones at lower indices or earlier in the enumeration than your current position).

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a string like this: La Torre Eiffel paragonata all&#8217;Everest What PHP function
I have this code to decode numeric html entities to the UTF8 equivalent character.
I have this code: - (void)parser:(NSXMLParser *)parser foundCDATA:(NSData *)CDATABlock { NSString *someString = [[NSString
This could be a duplicate question, but I have no idea what search terms
For some reason, after submitting a string like this Jack’s Spindle from a text
this is what i have right now Drawing an RSS feed into the php,
I'm parsing an RSS feed that has an &#8217; in it. SimpleXML turns this
I don't have much knowledge about the IPv6 protocol, so sorry if the question
I am trying to loop through a bunch of documents I have to put
I have a .ini file as follows: [playlist] numberofentries=2 File1=http://87.230.82.17:80 Title1=(#1 - 365/1400) Example

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.