Click here to Skip to main content
15,912,082 members
Home / Discussions / C#
   

C#

 
GeneralRe: error shown while working on xmltextreader Pin
exhaulted5-Jan-05 0:09
exhaulted5-Jan-05 0:09 
GeneralInstalling and Starting Services Pin
exhaulted4-Jan-05 22:55
exhaulted4-Jan-05 22:55 
GeneralOle Automation Pin
Jason Xie4-Jan-05 21:29
Jason Xie4-Jan-05 21:29 
GeneralRe: Ole Automation Pin
Heath Stewart4-Jan-05 21:56
protectorHeath Stewart4-Jan-05 21:56 
GeneralDifference between build and rebuild Pin
steve_rm4-Jan-05 20:34
steve_rm4-Jan-05 20:34 
GeneralRe: Difference between build and rebuild Pin
Heath Stewart4-Jan-05 21:36
protectorHeath Stewart4-Jan-05 21:36 
GeneralAsynchronous design question Pin
Yaakov Davis4-Jan-05 20:01
Yaakov Davis4-Jan-05 20:01 
GeneralRe: Asynchronous design question Pin
jan larsen4-Jan-05 21:14
jan larsen4-Jan-05 21:14 
sb777 wrote:
Are multimple threads allowed to access simultaneously the same method of an instance object?
Yes


sb777 wrote:
What happend then? they both run a different 'instance' of the method?
No, method variables lives on the stack, and each thread maintains its own stack. Example:
public class TestClass
{
   public static int Test(int i)
   {
      int retval = i;
      return retval;
   }
}


Let's assume that two different threads, tA and tB, invokes TestClass.Test:
tA pushes the 'i' integer argument on its stack, let's say the value is 42, and then it invokes
TestClass.Test.

tB pushes the 'i' integer argument on its stack, let's say the value is 9, and then it invokes TestClass.Test.

In both cases TestClass.Test makes room for the variable retval by pushing an integer to the stack of the current thread. It then sets the value of that integer to the value of i. And the value of i is found on the stack of the current thread. When you write 'return retval' you actually tells the compiler to push retval on the stack of the current thread.

So, in the example above, nothing can go wrong in a multithreaded environment. Things are getting ugly if the example looks like this though:

public class TestClass
{
   private static int j;

   public int J
   {
      get:
      {
         return j;
      }
      set:
      {
         j = value;
      }
   }

   public static int Test(int i)
   {
      int retval = i;
      return retval + j;
   }
}


Things are looking good right untill retval is added to j. The problem is that j lives on the heap, so both threads will access the same reference to j. This is where you, the programmer, needs to actually think (ugh!). At some point in you code, you must find the sequence of code that isn't threadsafe, and then you must wrap it in a lock block like this:
lock(aReferenceToAnObjectSharedByBothThreads)
{
   TestClass.J = x;
   int y = TestClass.Test(x + 1);
}


"After all it's just text at the end of the day. - Colin Davies

"For example, when a VB programmer comes to my house, they may say 'does your pool need cleaning, sir ?' " - Christian Graus
GeneralRe: Asynchronous design question Pin
Yaakov Davis4-Jan-05 22:47
Yaakov Davis4-Jan-05 22:47 
GeneralRe: Asynchronous design question Pin
jan larsen4-Jan-05 22:57
jan larsen4-Jan-05 22:57 
GeneralRe: Asynchronous design question Pin
Yaakov Davis5-Jan-05 2:38
Yaakov Davis5-Jan-05 2:38 
GeneralRe: Asynchronous design question Pin
jan larsen5-Jan-05 3:28
jan larsen5-Jan-05 3:28 
GeneralC# hangs while opening word in web application Pin
NituP4-Jan-05 19:53
NituP4-Jan-05 19:53 
GeneralRe: C# hangs while opening word in web application Pin
Heath Stewart4-Jan-05 21:53
protectorHeath Stewart4-Jan-05 21:53 
Generalmdi strange problem Pin
Gavin Jeffrey4-Jan-05 19:13
Gavin Jeffrey4-Jan-05 19:13 
QuestionDistributed System in "Javaspaces-Style"?? Pin
stumpi4-Jan-05 16:50
stumpi4-Jan-05 16:50 
AnswerRe: Distributed System in "Javaspaces-Style"?? Pin
Heath Stewart4-Jan-05 21:52
protectorHeath Stewart4-Jan-05 21:52 
GeneralBeginner Questions Pin
smitsky4-Jan-05 16:23
smitsky4-Jan-05 16:23 
GeneralRe: Beginner Questions Pin
Jay Shankar4-Jan-05 17:24
Jay Shankar4-Jan-05 17:24 
GeneralRe: Beginner Questions Pin
Mustafa Ismail Mustafa4-Jan-05 19:07
Mustafa Ismail Mustafa4-Jan-05 19:07 
Generalintegration with VS.NET 2003 Pin
Aryadip4-Jan-05 15:52
Aryadip4-Jan-05 15:52 
GeneralRe: integration with VS.NET 2003 Pin
Heath Stewart4-Jan-05 21:19
protectorHeath Stewart4-Jan-05 21:19 
GeneralC# Code Generators for Database Tier Pin
jareddavies4-Jan-05 15:45
jareddavies4-Jan-05 15:45 
GeneralRe: C# Code Generators for Database Tier Pin
Steven Campbell4-Jan-05 16:45
Steven Campbell4-Jan-05 16:45 
GeneralRe: C# Code Generators for Database Tier Pin
jareddavies5-Jan-05 16:38
jareddavies5-Jan-05 16:38 

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.