Results 1 to 5 of 5

Thread: lock Statement of C sharp

  1. #1
    Join Date
    Dec 2009
    Posts
    32

    lock Statement of C sharp

    Hello friends,

    I am new to the C sharp programming. I am ware about the basic statements of sharp. But "lock statement" seems very complicated to understand as I have never used this statement. I think this statement is may be used in multithreading concept but I am not sure on this. So please let me know about the "lock Statement" i.e about it's syntax, it's use and example. I am waiting for your reply.

  2. #2
    Join Date
    Apr 2008
    Posts
    2,005

    Re: lock Statement of C sharp

    Hi,

    In C sharp the 'lock Statement' is defined using 'lock' keyword, This type of c sharp statement to make critical section. This can be achieved by applying mutual exclusion lock over class object. After execution of statement block finally you need to release the applied lock. Following is the syntax for lock statement in C sharp:
    lock(expression) block_of_statement

  3. #3
    Join Date
    May 2008
    Posts
    2,297

    Re: lock Statement of C sharp

    Hi,

    If you want know about the use of lock Statement in C sharp, then please refer following C sharp example.
    Code:
    using System.Threading;
    using System;
    
    class ProgramDM
    {
        static readonly object _objectT = new object();
    
        static void AR()
        {
           
            lock (_objectT)
            {
                Thread.Sleep(100);
                Console.WriteLine(Environment.TickCount);
            }
        }
    
        static void Main()
        {
            
            for (int ix = 0; ix < 10; ix++)
            {
                ThreadStart start = new ThreadStart(AR);
                new Thread(start).Start();
            }
        }
    }

  4. #4
    Join Date
    Oct 2005
    Posts
    2,393

    Re: lock Statement of C sharp

    The lock Statement of C sharp is used only with the 'reference object', otherwise you can't use this statement. The functionality of this statement is identical to the critical functions of C sharp. The basic use of lock statement is to apply lock on critical code of program. lock statement make sure that thread doesn't enter a critical section when other thread is present in the critical section.

  5. #5
    Join Date
    May 2008
    Posts
    2,389

    Re: lock Statement of C sharp

    Below is the example of lock statement of C sharp, try to run this program and see what output it shows:
    Code:
    class AccountGH
    {   int balanceM;
       Random r = new Random();
       public Account(int initial) 
       {      balanceM = initial;
       }
       int Withdraw(int amount) 
       {    if (balanceM < 0) 
          {throw new Exception("Negative balanceM");   }
           lock (this)
          {
             if (balanceM >= amount) 
             {
                Console.WriteLine("balanceM before Withdrawal :  " + balanceM);
                Console.WriteLine("Amount to Withdraw        : -" + amount); 
                balanceM = balanceM - amount;
                Console.WriteLine("balanceM after Withdrawal  :  " + balanceM);
                return amount;
             }
             else 
             {     return 0; 
             }  }   }
    
       public void DoTransactions() 
       {
          for (int i = 0; i < 100; i++) 
          {
             Withdraw(r.Next(1, 100));
          }
       }
    }

Similar Threads

  1. Sharp 007SH Hybrid An 3D Clamshell Android Phone by Sharp
    By jackalz in forum Web News & Trends
    Replies: 3
    Last Post: 22-05-2011, 07:28 AM
  2. Issue with Dinovo Mini backslash, caps lock and num lock keys
    By Jarini in forum Hardware Peripherals
    Replies: 7
    Last Post: 02-11-2010, 02:27 AM
  3. Use of fixed statement in C sharp
    By Mithun Seth in forum Software Development
    Replies: 5
    Last Post: 02-02-2010, 01:27 PM
  4. Foreach statement of C sharp
    By Ram Bharose in forum Software Development
    Replies: 5
    Last Post: 19-01-2010, 08:42 AM
  5. Replies: 4
    Last Post: 25-02-2009, 08:52 PM

Tags for this Thread

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Page generated in 1,713,505,153.99894 seconds with 16 queries