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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T07:51:32+00:00 2026-05-27T07:51:32+00:00

I am working through a Parallel Programming example on Race conditions In the example

  • 0

I am working through a Parallel Programming example on Race conditions

In the example they are demonstrating the isolation patter to deal with race conditions

Why is it in this example following that a race condition does not occur when creating the task and the stateObject is passed as part of the task creation

I understand that we use isolatedBalance to do the updateing …but at the point where we assign the isolatedbalance = (int)stateObject could not another tasks finished balance be there i.e not 0 but 100 ???

So if there where enough tasks and that the task scheduler started an early task and it finished at a point when a later task is being created and assinged the account.Balance value would be 100 etc for when 1 of the tasks had finshed for a taks that was starting

class BankAccount
{
    public int Balance { get; set; } 
}

class Program
{
    static void Main(string[] args)
    {

        var account = new BankAccount();
        var tasks = new Task<int>[1000];


        for (int i = 0; i < 1000; i++)
        {
            tasks[i] = new Task<int>((stateObject)=>
            {
                int isobalance = (int) stateObject;
                for (int j = 0; j < 1000; j++)
                {
                    isobalance ++;
                }
                return isobalance;
             }, account.Balance);

             tasks[i].Start();
        }
        Task.WaitAll(tasks);

        for (int i = 0; i < 1000; i++)
        {
            account.Balance += tasks[i].Result;
        }

        Console.WriteLine("Epectecd valeu {0}, Counter value {1}",1000000,account.Balance);

        // wait for input before exiting
        Console.WriteLine("Press enter to finish");
        Console.ReadLine();
    }
}
  • 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-05-27T07:51:33+00:00Added an answer on May 27, 2026 at 7:51 am

    The method that you have passed to the Task constructor does not update account.Balance it only uses the initial value of account.Balance. It does not update it. int is pass by value. From MSDN:

    A value-type variable contains its data directly as opposed to a reference-type variable, which contains a reference to its data. Therefore, passing a value-type variable to a method means passing a copy of the variable to the method. Any changes to the parameter that take place inside the method have no affect on the original data stored in the variable. If you want the called method to change the value of the parameter, you have to pass it by reference, using the ref or out keyword. For simplicity, the following examples use ref.

    Therefore account.Balance is not updated until after Task.WaitAll(tasks); is called. Task.WaitAll() causes the code to stop there until all tasks have finished. Only after that, once all the results have been computed. will account.Balance be updated with the values returned from tasks[i].Result.

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

Sidebar

Related Questions

Working through a sample in Chapter 3 of Programming in Scala, the following code
Working through Pro ASP.NET MVC book and I got the following code snippet that
Working through this for fun: http://www.diku.dk/hjemmesider/ansatte/torbenm/Basics/ Example calculation of nullable and first uses a
I'm working through previous years ACM Programming Competition problems trying to get better at
I'm currently working through an assignment that deals with Big-O and running times. I
I am working through some of the exercises in The C++ Programming Language by
I am working on a experimental website (which is accessible through web browser) that
Working through more book examples- this one is a partial poker program- This segment
Working through several layers of an MVC architecture designed program, I find that I
Having problems compiling this class. I'm working through a book on java and this

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.