Note that there are some explanatory texts on larger screens.

plurals
  1. POParallel Programming Race Conditions
    primarykey
    data
    text
    <p>I am working through a Parallel Programming example on Race conditions </p> <p>In the example they are demonstrating the isolation patter to deal with race conditions</p> <p>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 </p> <p>I understand that we use isolatedBalance to do the updateing ...but at the point where we assign the <code>isolatedbalance = (int)stateObject</code> could not another tasks finished balance be there i.e not 0 but 100 ???</p> <p>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 </p> <pre><code>class BankAccount { public int Balance { get; set; } } class Program { static void Main(string[] args) { var account = new BankAccount(); var tasks = new Task&lt;int&gt;[1000]; for (int i = 0; i &lt; 1000; i++) { tasks[i] = new Task&lt;int&gt;((stateObject)=&gt; { int isobalance = (int) stateObject; for (int j = 0; j &lt; 1000; j++) { isobalance ++; } return isobalance; }, account.Balance); tasks[i].Start(); } Task.WaitAll(tasks); for (int i = 0; i &lt; 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(); } } </code></pre>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload