-----------------------------------------------------------

New Message on BDOTNET

-----------------------------------------------------------
From: Sitaraman
Message 1 in Discussion

Hi All   Was trying the lock and Monitor.Enter/Exit and have a few doubts in that.  
lets say i have a class which has a static method. Now this static method increments a 
static counter variable. Multiple Threads access this method.  I want to have thread 
safety.  I use either the lock keyword or the Monitor Enter/Exit methods.  The 
code(using lock keyword) is as follows      using System;
using System.Threading; public class StaticThreads 
{
 public static String strCounter="0";
 public static int intCounter=0;  public static void Main() 
 {
  Thread myThread1 = new Thread(new
   ThreadStart(StaticThreads.DoSomething));
  Thread myThread2 = new Thread(new
   ThreadStart(StaticThreads.DoSomething));
  myThread1.Name = "Thread 1";
  myThread2.Name = "Thread 2";
  Console.WriteLine("Press ENTER to start,");
  Console.WriteLine("and ENTER again to stop.");
  Console.ReadLine();
  myThread1.Start();
  myThread2.Start();
  Console.ReadLine();
  myThread1.Abort();
  myThread2.Abort();
  
  
 }   

    public static void DoSomething()  
 {
  while (true) 
  {
   lock(typeof(StaticThreads))//Acquiring a lock 
   {
    //declare a local variable
    String i1; 
                // get counter value, increment and store it in local var(not set in 
static var yet)
    i1=Convert.ToString((Convert.ToInt16(StaticThreads.strCounter)+1)); 
    // Wait.  equivalent to some processing in real world 
    Thread.Sleep(1000); 
    // Setting the new value in the static variable
    StaticThreads.strCounter=i1; 
    // printing the modified counter value
    Console.WriteLine(Thread.CurrentThread.Name + " New Counter Value : " +  
StaticThreads.strCounter );
   } // releasing the lock 
  }
 } 
 
}    My questions are           Why do i have to pass typeof(StaticThreads) and not 
pass just StaticThreads.strCounter. Is it coz of the fact that DoSomething()  is a 
static method.  how does it make a difference.  Also wat is the impact on performance 
when i pass typeof.  Is the duration of the lock lengthier in the case of passing 
typeof(StaticThreads) 
        I tried the above code using Monitor.Enter/Exit methods.  the output is same. 
Even MSDN says that lock and Monitor.Enter/Exit is precisely equivalent "except that 
passed object parameter is only evaluated once".  What does this mean.  What is the 
impact
        Why is that these mechanisms take only reference type parameter and not value 
type parameter.  is it basically to avoid performance overhead which would be there 
due to boxing/unboxing.  If i do want to pass a value type, do i have to have a 
wrapper object on such value types for passing to these mechanisms.  Also the error on 
passing value types to these mechanisms are quite weird.  Whereas lock gives me a 
compiler error when a value type is passed,  the Monitor.nter and Exit gives me 
Runtime Exception (" System.Threading.SynchronizationLockException"). Why is this so?  
   regards,   sr   p.s. 1)  i do not want ro use singleton(as the example is the 
standard one for singleton mechanisms) 2) i have already checked the C# Language 
Specification   8.12 The lock statement (just in case one of u pt me to tht :) )

-----------------------------------------------------------

To stop getting this e-mail, or change how often it arrives, go to your E-mail 
Settings.
http://groups.msn.com/BDotNet/_emailsettings.msnw

Need help? If you've forgotten your password, please go to Passport Member Services.
http://groups.msn.com/_passportredir.msnw?ppmprop=help

For other questions or feedback, go to our Contact Us page.
http://groups.msn.com/contact

If you do not want to receive future e-mail from this MSN group, or if you received 
this message by mistake, please click the "Remove" link below. On the pre-addressed 
e-mail message that opens, simply click "Send". Your e-mail address will be deleted 
from this group's mailing list.
mailto:[EMAIL PROTECTED]

Reply via email to