1

I am reading a book about C#, I know what a critical section is used for, however this example was on the book and it confuses me:

public bool BankTransferWithMutex(int amount)
{
    bool result = false;

    MyMutex.WaitOne();

    if (Balance >= amount)
    {
        Balance -= amount;
        result = true;
    }

    MyMutex.ReleaseMutex();
    //My question is here..
    return result;
    }
}

My question is this, imagine there were two threads, one of them obtained access to the mutex and the bank transfer succeeded putting the result variable to true.. if the other thread came along (before the first one does the return and entered this method it would put result = false right away. Would the first thread have is result variable modified and therefore would return false despite the bank transfer being successful? Making the object state inconsistent??

Thank you for your time :)

DavidN
  • 111
  • 1
  • 9
  • `result` is a local variable not a shared one. Can you tell me what the book do you read? I suggest you to read other book. – Hamlet Hakobyan Jan 31 '15 at 15:35
  • Result is a **local** variable without any _reference_, pointer or whatever to it outside your function. It cannot be modified by another thread. – Adriano Repetti Jan 31 '15 at 15:35
  • But the Mutex is there because of the variable Balance, and that is a shared variable right? – DavidN Jan 31 '15 at 15:44
  • 1
    You haven't shown the code that defines Balance, so we don't know if it's shared - but presumably yes, otherwise the example makes no sense. By the way, the example is strange in having an argument "BankAccount anotherAccount" that is not used. Or have you not posted the entire example? – RenniePet Jan 31 '15 at 16:07
  • 1
    As for the variable "result", you have to take into consideration that it is a local variable so there is a "result" for every invocation of BankTransferWithMutex(). If there are 10 threads and they all happen to be in BankTransferWithMutex() at the same time then there are 10 distinct "result" variables. – RenniePet Jan 31 '15 at 16:10
  • The argument isn't supposed to be there.. sorry :/ I'm going to edit it out. The Balance is just defined as a global variable like this: class BankAccount { int Balance; ... But what defines a shared variable? I thought it was just a variable that two threads could have access to, but this would make result also a shared one which in reality isn't.. – DavidN Jan 31 '15 at 16:17
  • Hmmm I see why I was getting it all wrong, thank you very much for the help! – DavidN Jan 31 '15 at 16:34

1 Answers1

1

Here's my attempt to post an answer based on your additional comments and on your indication that you considered one of my comments as helpful.

Assuming the code you're looking at is somewhat similar to this:

   public class BankAccount
   {
      private int Balance;

      private Mutex MyMutex = new Mutex();


      public bool BankTransferWithMutex(int amount)
      {
         bool result = false;

         MyMutex.WaitOne();

         if (Balance >= amount)
         {
            Balance -= amount;
            result = true;
         }

         MyMutex.ReleaseMutex();
         //My question is here..
         return result;
      }
   }

then a fundamental difference between Balance and result is that Balance is an class instance variable, and there is one instance of it for each instance of the class BankAccount. Meanwhile, result is a local variable so there is one instance of result for each existing (usually zero) invocations of the BankTransferWithMutex() method.

So if there is one instance of a BankAccount object, and it is being shared by 10 threads, then the 10 threads are sharing the one instance of Balance. The result variables are associated with the invocations of the BankTransferWithMutex() method, so there is one for each current invocation - usually zero but can be up to 10.

Here's an MSDN link, although it requires clicking through a bunch to sub-topics to see the whole thing: https://msdn.microsoft.com/en-us/library/aa691160%28v=vs.71%29.aspx

RenniePet
  • 11,420
  • 7
  • 80
  • 106