什么是条件同步? [英] What is condition synchronization?

查看:236
本文介绍了什么是条件同步?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

有人能解释一下条件同步给我?

Could someone explain condition synchronization to me?

这是例子(最好在C#)也将不胜感激。

An example (preferably in C#) would be greatly appreciated also.

推荐答案

这听起来像你的教授都在谈论线程。线程使计算机程序在同一时间做多件事情。开始一个新的线程,而一个已经运行被称为纺纱一个线程由计算机程序员的行为。

It sounds like your professor is talking about threading. Threading enables computer programs to do more than one thing at a time. The act of starting a new thread while one is already running is called "spinning up a thread" by computer programmers.

线程可以共享相同的内存空间。条件同步(或仅仅同步)是保护的存储器区域从在同一时间由两个不同的线程被修改的任何机制。

Threads can share the same memory space. Condition Synchronization (or merely synchronization) is any mechanism that protects areas of memory from being modified by two different threads at the same time.

让我们说你是做出来购物,而妻子在家付账。这是一个简单的例子,它并没有真正的工作就这样在现实生活中,但它会作为一个简单的例子。

Let's say you are out doing shopping, and the wife is at home paying the bills. This is a naive example, and it doesn't really work this way in real life, but it will serve as a simple illustration.

您的妻子支付账单在线。与此同时,你在杂货店刷你的信用卡。这两种行为涉及移动钱拿出您的支票帐户。为了模拟这一活动,我们编写以下代码:

Your wife is paying a bill online. At the same time, you are swiping your credit card at the grocery store. Both acts involve moving money out of your checking account. To simulate this activity, we write the following code:

public class MyBanking
{
    static double myAccountBalance;
    //
    public void DebitAccount(double debitAmount)
    {
        Console.Writeline("Your Old Balance is: " + myAccountBalance.ToString());
        Console.Writeline("Your Debit is:       " + debitAmount.ToString());
        myAccountBalance = myAccountBalance - amount;
        Console.Writeline("Your New Balance is: " + myAccountBalance.ToString());
    }
}



可以想像,你的妻子正在运行一个实例(拷贝这个类的一个线程),你正在运行的另一个线程的实例。该myAccountBalance变量声明为static,使其能够对运行实例之间共享(你和你的妻子只有一个支票帐户)。

Hypothetically, your wife is running one instance ("copy") of this class on one thread, you are running an instance on another thread. The myAccountBalance variable is declared static to allow it to be shared between both running instances (you and your wife have only one checking account).

您通过调用使您的扣款这样的代码:

You make your debit by calling the code like this:

MyBanking bankingObject = new MyBanking();
bankingObject.DebitAccount(100);

您的妻子让她借记卡在同一时间:

Your wife makes her debit at the same time:

MyBanking bankingObject = new MyBanking();
bankingObject.DebitAccount(50);

如果你的线程得到由你妻子的线程旧的平衡被印在屏幕上后中断,会发生什么,但新的平衡之前打印?你妻子的线程借记帐户,并把控制返回到你的线程。你的妻子看到这个在屏幕上:

What happens if your thread gets interrupted by your wife's thread after your old balance is printed on the screen, but before the new balance is printed? Your wife's thread debits the account and returns control back to your thread. Your wife sees this on the screen:

Your Old Balance is: 2000
Your Debit is:       50
Your New Balance Is: 1950

在计算机打印屏幕上的新的平衡,这将是错了,因为你妻子的借记卡将被也算。你会看到这样的事情:

When the computer prints the new balance on your screen, it will be wrong, because your wife's debit will have been counted also. You will see something like this:

Your Old Balance is: 2000
Your Debit is:       100
Your New Balance Is: 1850

要解决这个问题,我们围绕我们的方法代码lock语句。 lock语句导致所有其他线程等待我们的实例来完成新的代码如下所示:

To fix this, we surround our method code with a lock statement. The lock statement causes all other threads to wait for our instance to finish. The new code looks like this:

public class MyBanking
{
    static double myAccountBalance;
    //
    public void DebitAccount(double debitAmount)
    {
        lock (this)
        {
            Console.Writeline("Your Old Balance is: " + myAccountBalance.ToString());
            Console.Writeline("Your Debit is:       " + debitAmount.ToString());
            myAccountBalance = myAccountBalance - amount;
            Console.Writeline("Your New Balance is: " + myAccountBalance.ToString());
        }
    }
}

您妻子的线程现在等待换锁语句中的代码来完成你妻子的代码之前执行,开始执行。你的新的平衡现在是正确的,因为不再是你妻子的线程的改变,而你完成你的交易余额的可能性。在你的屏幕上,你会看到这一点:

Your wife's thread will now wait for your code within the lock statement to finish executing, before your wife's code begins executing. Your new balance will now be correct, because there is no longer the possibility of your wife's thread changing the balance while you complete YOUR transaction. On your screen, you will now see this:

Your Old Balance is: 2000
Your Debit is:       100
Your New Balance Is: 1900

您的妻子会看到这一点:

Your wife will see this:

Your Old Balance is: 1900
Your Debit is:       50
Your New Balance Is: 1850

这是同步的。

这篇关于什么是条件同步?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆