. NET parallel (multicore) programming series seven shared data issues and solutions overview

Source: Internet
Author: User

Preface: The previous article introduced some basic knowledge of parallel programming, from this beginning, will talk about the actual encounter some problems in parallel programming, the next few will talk about data sharing problem.

The topics in this article are as follows:

Data competition

Solution proposal

Sequential Execution Solution

Data invariant Solutions

Before we begin, let's take a look at an interesting example:

 class BankAccount 
{
public int Balance
{
get;
Set
}
}
class App
{
static void Main (string[] args)
{
//Create the bank account ins Tance
BankAccount account = new BankAccount ();
Create an array of tasks
task[] tasks = new TASK[10];
for (int i = 0; i < i++)
{
///Create a new task
Tasks[i] = new Task (() =&G T
{
//Enter a loop for 1000 balance updates
for (int j = 0; J < 10 00; J + +)
{
//update the Balance
account. Balance = account. Balance + 1;
}
});
//Start the new task
Tasks[i]. Start ();
}
//Wait for all of the tasks to complete
Task.waitall (tasks);
Write out the counter value
Console.WriteLine ("Expected value {0}, Counter value: {1}",
10000, account. Balance);
//wait for input before exiting
Console.WriteLine ("Press ENTER to finish");
Console.ReadLine ();
}
}

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.