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 8645353
In Process

The Archive Base Latest Questions

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

using System.Threading.Tasks; const int _Total = 1000000; [ThreadStatic] static long count = 0; static

  • 0
using System.Threading.Tasks;

const int _Total = 1000000;
[ThreadStatic]
static long count = 0;

static void Main(string[] args)
{
    Parallel.For(0, _Total, (i) =>
    {
        count++;
    });

    Console.WriteLine(count);
}

I get different result every time, can anybody help me and tell me why?

  • 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:31:48+00:00Added an answer on June 12, 2026 at 12:31 pm

    Most likely your “count” variable isn’t atomic in any form, so you are getting concurrent modifications that aren’t synchronized. Thus, the following sequence of events is possible:

    • Thread 1 reads “count”
    • Thread 2 reads “count”
    • Thread 1 stores value+1
    • Thread 2 stores value+1

    Thus, the “for” loop has done 2 iterations, but the value has only increased by 1. As thread ordering is “random”, so will be the result.

    Things can get a lot worse, of course:

    • Thread 1 reads count
    • Thread 2 increases count 100 times
    • Thread 1 stores value+1

    In that case, all those 100 increases done by thread 2 are undone. Although that can really only happen if the “++” is actually split into at least 2 machine instructions, so it can be interrupted in the middle of the operation. In the one-instruction case, you’re only dealing with interleaved hardware threads.

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

Sidebar

Related Questions

using System; using System.Threading; using System.Threading.Tasks; class Program { static void Main() { double[]
I am using System.Threading.Tasks.Parallel.For to do some heavyweight processing. My code is: int count
The Code using System; using System.Threading; public delegate void LoadingProgressCallback(double PercentComplete,string ItemName); public delegate
I am using System.Threading.Tasks to execute my long running service calls and using ContinueWith
using System.Collections.Generic; using System.Threading; using System.Threading.Tasks; public sealed class Foo { public int ID;
using System; using System.Collections.Generic; using System.Text; using System.Threading.Tasks; namespace ConsoleApplication1 { class Program {
If I am using System.Threading.Task to run parallel tasks like this: Func<MyResult> func=ComputeResult; var
When using System.Threading.Timer, and initializing a windows form on the timer's tick event, the
I am using System.Threading.ThreadPool.QueueUserWorkItem(x => MyMethod(param1, param2, param3, param4, param5)); I want to call
using System; using System.Drawing; using System.IO; using System.Security.Cryptography; using System.Text.RegularExpressions; using System.Windows.Forms; using System.Threading;

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.