-----------------------------------------------------------
New Message on BDOTNET
-----------------------------------------------------------
From: Sitaraman
Message 1 in Discussion
Hi Was experimenting with the lock keyword and need some help. I basically have a
counter variable(String type as lock accepts only reference types). I have 2 threads
that increment this counter variable. My requirement is that i should ensure that
only one threadmodifies the value at a point of time. So i used the lock
keyword(optionally Monitor.Enter/Exit) to mark it as a critical section
<o:p></o:p>using System;<o:p></o:p>
using System.Threading;<o:p></o:p>
<o:p></o:p>
public class StaticThreads <o:p></o:p>
{<o:p></o:p>
public static String strCounter="0";<o:p></o:p>
public static void Main() <o:p></o:p>
{<o:p></o:p>
Thread myThread1 = new Thread(new<o:p></o:p>
ThreadStart(StaticThreads.DoSomething));<o:p></o:p>
Thread myThread2 = new Thread(new<o:p></o:p>
ThreadStart(StaticThreads.DoSomething));<o:p></o:p>
myThread1.Name = "Thread 1";<o:p></o:p>
myThread2.Name = "Thread 2";<o:p></o:p>
Console.WriteLine("Press ENTER to start,");<o:p></o:p>
Console.WriteLine("and ENTER again to stop.");<o:p></o:p>
Console.ReadLine();<o:p></o:p>
myThread1.Start();<o:p></o:p>
myThread2.Start();<o:p></o:p>
Console.ReadLine();<o:p></o:p>
myThread1.Abort();<o:p></o:p>
myThread2.Abort();<o:p></o:p>
<o:p></o:p>
<o:p></o:p>
}<o:p></o:p>
<o:p></o:p>
public static void DoSomething() <o:p></o:p>
{<o:p></o:p>
while (true) <o:p></o:p>
{<o:p></o:p>
lock(typeof(StaticThreads))//Acquiring a lock <o:p></o:p>
{<o:p></o:p>
//declare a local variable<o:p></o:p>
String i1; <o:p></o:p>
// get counter value, increment and store it in local
var(not set in static var yet)<o:p></o:p>
i1=Convert.ToString((Convert.ToInt16(StaticThreads.strCounter)+1)); <o:p></o:p>
// Wait. equivalent to some processing in real world
<o:p></o:p>
Thread.Sleep(1000); <o:p></o:p>
// Setting the new value in the static variable<o:p></o:p>
StaticThreads.strCounter=i1; <o:p></o:p>
// printing the modified counter value<o:p></o:p>
Console.WriteLine(Thread.CurrentThread.Name + " New
Counter Value : " + StaticThreads.strCounter );<o:p></o:p>
} // releasing the lock <o:p></o:p>
}<o:p></o:p>
}<o:p></o:p>
<o:p></o:p>
<o:p></o:p>
<o:p></o:p>
}
Here i have 3 doubts While using lock() why do i have to pass
typeof(StaticThreads) and not StaticThreads.strCounter. Is it coz the method
DoSomething is static. how does it make a difference?
What exactly is the difference btwn using the lock and Monitor.Enter/Exit.
MSDN says that lock is exactly like Monitor.Enter/Exit "except that the passed object
is only evaluated once.". wat does it mean
Why doesnt the lock or Monitor.Enter/Exit allow value types to be passed. Is
it coz of boxing/unboxing overhead. On top of it, the errors i get also are different
if i use value type variables. lock gives me compile error and Monitor.Enter/Exit
gives me "System.Threading.SynchronizationLockException".
would appreciate any comments regards, sr
-----------------------------------------------------------
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]