Object threading

  • Thread starter Thread starter news.microsoft.com
  • Start date Start date
N

news.microsoft.com

Hi,


If I have an object taht has a method thats started as a tread and other
methods that are called by other classes.
WHat is the boundries? The methods called by other claseses are in one
thread and the threadproc in another, so we must sync those objects taht are
being shared between the called methods and the thread proc? Right?

SO basically the object itself is sitting in the caller thread, yet the
threadproc inside the object is in another. so its feet in both threads?

Thanks
 
news.microsoft.com said:
If I have an object taht has a method thats started as a tread and other
methods that are called by other classes.
WHat is the boundries? The methods called by other claseses are in one
thread and the threadproc in another, so we must sync those objects taht are
being shared between the called methods and the thread proc? Right?

SO basically the object itself is sitting in the caller thread, yet the
threadproc inside the object is in another. so its feet in both threads?

Objects don't "sit" in threads at all. I'm not sure what you mean by
this. If data needs to be shared between threads, you need to
synchronize in order to make sure that the most up-to-date version is
seen, and to avoid race conditions etc.
 
An object is allocated on the heap, the heap is shared for your current
application and in effect for all your applications threads.

So if you create an object on the heap, all your threads could share that
object and it's state. This means that if your Main thread (that every
application has) creates an object on the heap and spawns a thread that
executes a method on that object, the Main threads operation on that object
could be affected by these changes since it looks at the same data.

If you want to share an object across threads, you need to be careful in how
you handle instance fields, global object data, since all threads will
operate on the same copy of that field. This is also true for static fields.

Although when it comes to local variables, it's a totally different story
since localv ariables are stored on the stack and , in contrast to the heap,
are thread specific. Meaning that every thread has it's own copy of the
variable.
 
Patrik Löwendahl said:
An object is allocated on the heap, the heap is shared for your current
application and in effect for all your applications threads.

So if you create an object on the heap, all your threads could share that
object and it's state. This means that if your Main thread (that every
application has) creates an object on the heap and spawns a thread that
executes a method on that object, the Main threads operation on that object
could be affected by these changes since it looks at the same data.

If you want to share an object across threads, you need to be careful in how
you handle instance fields, global object data, since all threads will
operate on the same copy of that field. This is also true for static fields.

Although when it comes to local variables, it's a totally different story
since localv ariables are stored on the stack and , in contrast to the heap,
are thread specific. Meaning that every thread has it's own copy of the
variable.
 
I know about threading, just not how C# would treat it with regard to an
object method being started as a thread and that method sitting in an object
etc. basically as I see it only that method is in a thread and the variables
local to that method are in that thread but the rest is shared, so how would
a static method be different if any?
 
news.microsoft.com said:
I know about threading, just not how C# would treat it with regard to an
object method being started as a thread and that method sitting in an object
etc. basically as I see it only that method is in a thread and the variables
local to that method are in that thread but the rest is shared, so how would
a static method be different if any?

Not different at all.
 
If you are interested in theory of threading and concurrent systems - check
google for concurrent systems - there is a wealth of information available.
If you have some practical issue at hand it would be more productive here to
discuss this specific issue

HTH
Alex
 
Back
Top