Click here to Skip to main content
15,888,610 members
Home / Discussions / C#
   

C#

 
Questionhow can i suppress Microsoft .NET security warning. [modified] Pin
Dattatraya K15-Jan-09 23:09
Dattatraya K15-Jan-09 23:09 
AnswerRe: how can i suppress Microsoft .NET security warning. Pin
SeMartens15-Jan-09 23:17
SeMartens15-Jan-09 23:17 
GeneralRe: how can i suppress Microsoft .NET security warning. Pin
Dattatraya K15-Jan-09 23:20
Dattatraya K15-Jan-09 23:20 
GeneralRe: how can i suppress Microsoft .NET security warning. Pin
SeMartens15-Jan-09 23:35
SeMartens15-Jan-09 23:35 
GeneralRe: how can i suppress Microsoft .NET security warning. Pin
Dave Kreskowiak16-Jan-09 4:01
mveDave Kreskowiak16-Jan-09 4:01 
QuestionProgramatically setting the smtp server in Outlook 2003 Pin
WritinCode15-Jan-09 22:51
WritinCode15-Jan-09 22:51 
AnswerRe: Programatically setting the smtp server in Outlook 2003 Pin
campurr3-Apr-10 5:44
campurr3-Apr-10 5:44 
QuestionSynchronization via Locks Pin
the_jat15-Jan-09 21:57
the_jat15-Jan-09 21:57 
In the book C# 3.0 in a Nutshell, chapter on threading, the author while explaining the locks, says:

"The following class is not thread-safe: if Go was called by two threads simultaneously, it would be possible to get a division-by-zero error, because val2 could be set to zero in one thread right as the other thread was in between executing the if statement and Console.WriteLine."
class ThreadUnsafe
{
  static int val1, val2;

  static void Go(  )
  {
    if (val2 != 0) Console.WriteLine (val1 / val2);
    val2 = 0;
  }
}

The author then gives the following class and says that:
"In this case, we're protecting the logic inside the Go method, as well as the fields val1 and val2."
class ThreadSafe
{
  static object locker = new object(  );
  static int val1, val2;

  static void Go(  )
  {
    lock (locker)
    {
      if (val2 != 0) Console.WriteLine (val1 / val2);
      val2 = 0;
    }
  }
}

My question:
I get that two threads cannot enter the lock construct at the same time, but still one thread can(I guess) modify the static variables as the other is executing the if statement, which would again lead to a division by zero error. What I dont' understand is that how this lock construct protects the static variables from being modified by other threads.
Furthermore, if the static variables can be modified, what is the use of this lock construct and what exactly the author meant?

Thanks Smile | :)
AnswerRe: Synchronization via Locks Pin
SeMartens15-Jan-09 22:18
SeMartens15-Jan-09 22:18 
GeneralRe: Synchronization via Locks Pin
the_jat15-Jan-09 22:44
the_jat15-Jan-09 22:44 
GeneralRe: Synchronization via Locks Pin
N a v a n e e t h15-Jan-09 22:48
N a v a n e e t h15-Jan-09 22:48 
GeneralRe: Synchronization via Locks Pin
the_jat15-Jan-09 22:57
the_jat15-Jan-09 22:57 
AnswerRe: Synchronization via Locks [modified] Pin
Luc Pattyn16-Jan-09 1:53
sitebuilderLuc Pattyn16-Jan-09 1:53 
AnswerRe: Synchronization via Locks Pin
CPallini15-Jan-09 22:21
mveCPallini15-Jan-09 22:21 
GeneralRe: Synchronization via Locks Pin
the_jat15-Jan-09 22:32
the_jat15-Jan-09 22:32 
QuestionRe: Synchronization via Locks Pin
CPallini15-Jan-09 23:25
mveCPallini15-Jan-09 23:25 
AnswerRe: Synchronization via Locks Pin
the_jat15-Jan-09 23:43
the_jat15-Jan-09 23:43 
GeneralRe: Synchronization via Locks Pin
CPallini16-Jan-09 0:30
mveCPallini16-Jan-09 0:30 
AnswerRe: Synchronization via Locks Pin
N a v a n e e t h15-Jan-09 22:44
N a v a n e e t h15-Jan-09 22:44 
GeneralRe: Synchronization via Locks Pin
the_jat15-Jan-09 22:51
the_jat15-Jan-09 22:51 
GeneralRe: Synchronization via Locks Pin
SeMartens15-Jan-09 23:15
SeMartens15-Jan-09 23:15 
GeneralRe: Synchronization via Locks Pin
SeMartens15-Jan-09 23:24
SeMartens15-Jan-09 23:24 
GeneralRe: Synchronization via Locks Pin
the_jat15-Jan-09 23:32
the_jat15-Jan-09 23:32 
QuestionCan I programmatic enumerate running processes? Pin
devvvy15-Jan-09 20:18
devvvy15-Jan-09 20:18 
AnswerRe: Can I programmatic enumerate running processes? Pin
J a a n s15-Jan-09 20:36
professionalJ a a n s15-Jan-09 20:36 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.