views:

281

answers:

1

Please could someone explain condition synchronization to me? Having a hard time finding an example that explains itself well enough due to rubbish lecturing material.

An example would be greatly appreciated also - preferably in C#

Kind regards

+5  A: 

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());
    }
}

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

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: 1800

This is synchronization.

Robert Harvey