<$BlogRSDUrl$>

Monday, September 20, 2004

Google Search: thread safe methods dotnet 

Some fundamentals on thread safety:
I hesitate to jump in on this one since it needs a full treatise on what
threads are and how method calls are implemented but I will give you a very
brief overview.

Shared methods and instance methods are no different when it comes to how
they run or their thread safety (or lack thereof). A method is just the
address of some instructions to be executed by the CPU and calling a method
just involves placing some values(parameters) on the stack and then jumping
to that address. The only difference between a shared and an instance method
is that the first thing placed on the stack in an instance call is the
address of the instance, so that its data is available. All calls to shared
method A jump to the same address; all calls to instance method B on
different instances of a class jump to the same address but they have
different instance addresses put on the stack that point to the particular
object instance. Method code is never copied, whether shared or instance.

When a method is called on a particular thread the parameters are placed on
the thread's stack (each thread has it's own stack) and then execution jumps
to the first instruction of the method. If the thread is suspended and
another thread calls the same method it doesn't know anything about the
first thread it just goes ahead and puts the parameters on the stack and
jumps to the method's first instruction. But here's the key point - all the
parameters (and any local variables) are on two different stacks and all
references in the method to this data is relative to the threads' stack
pointers. So the separate threads can never access each others local data.
The only way there can be blocking is if a specific blocking instruction is
inserted in the method (such as a Monitor, etc.). So methods that only
access parameters and local variables are automatically thread safe
(provided, of course, that any methods they call out to are similarly thread
safe).

The problem comes when a method accesses module level data (whether shared
or instance data) in this case the address to the data could be the same on
both stacks and the threads would then be in danger of corrupting the data
depending on whether or no the address is written to and on the order the
instructions in the method are executed. This is where thread
synchronization methods need to be used.

There is no magic method to ensure thread safety each individual situation
can be significantly different. Also, it is dangerous to focus on making
methods thread safe, it isn't the method that needs to be thread safe but
the access to the module level data and this can, and usually does, involve
multiple methods. In addition, some methods you don't want to be thread safe
since it can impact performance.

Multi-threaded programming in .NET appears to be simple but anything beyond
the trivial requires significant research.

===========
I found all the above at following URL:

Google Search: thread safe methods dotnet

Comments: Post a Comment

This page is powered by Blogger. Isn't yours?