> I need to run a peice of code without any interruptionsby other thread.
> In other words I want to put a mutual exclusion lock.
You should be very clear about what you're trying to do. Your first
sentence isn't necessarily the same as the second.
In particular, there is no way to run a given thread without being
interrupted by another thread. Windows will preempt a thread that has
exhausted its timeslice, assuming another thread of equal or higher
priority is ready to run, no matter what sort of locking you do.
On the other hand, it's fairly common to use some kind of lock to ensure
"mutual exclusion" for a given section of code, or for a given piece of
data. There are a variety of ways to do this, and indeed the "lock()"
statement and the Monitor class both can be used to do that (my
understanding is that the "lock()" statement is actually implemented
using the Monitor class).
So...
> [...]
> I read a microsoft document where it mentions that I can use Lock() method
> and Monitor() method to put locks,
> but I am not sure how to call a method inside lock method in order to
> execute this getBytes() method that resides in a class library.
The first step is to decide what it is you want to ensure mutually
exclusive access to. If there is a specific section of code that you
want to ensure only one thread is executing at a time, you would put
that code inside the protected block. With the lock() statement, it
would look something like this:
class MyClass
{
object _objLock = new object();
void MyMethod()
{
lock(_objLock)
{
// protected code here
}
}
}
The "_objLock" object is an arbitrary instance that is used to protect
that section of code.
Using Monitor, the above code might look more like:
class MyClass
{
object _objLock = new object();
void MyMethod()
{
Monitor.Enter(_objLock);
try
{
// protected code here
}
finally
{
Monitor.Exit(_objLock);
}
}
}
If you want to protect data rather than a specific section of code, then
all of the code that might access that data needs to similarly using
some form of locking. If you only access that data in one place, then
that's the same scenario as protecting a specific section of code, of
course.
Please keep in mind that none of this locking prevents one thread from
interrupting another per se. If one thread tries to acquire a lock
already held by another thread, it will in fact wait for that other
thread. So in that sense, you can prevent a specific thread from
interrupting another. But until a thread tries to get that lock, it's
not prevented from running.
In fact, because of this one risk of locking is "deadlock". This
happens when you have two or more threads that each want exclusive
access to two or more separate resources at the same time, but don't
acquire them in the same order. You can wind up with each thread
waiting for the other to release a resource that it will not release
until that other thread gets the resource the original thread is holding.
In other words, if you start writing multi-threaded code that does some
sort of synchronization, be careful. :)
Pete
> Hello Everyone,
>
[quoted text clipped - 26 lines]
>
> Thanks.
Are you looking for something like:
SomeObject obj = new SomeObject();
byte[] bytes = null;
lock (obj)
{
bytes = obj.GetBytes();
}
// do cool stuff
--
Tom Shelton
Vinki - 09 Oct 2007 06:09 GMT
Hi,
Actually I cannot use SomeObject obj = new SomeObject(); or object
_objLock = new object(); since I am calling a static method in that class so
my call is like below :
classLibrary1.class1.GetBytes();
that's how I am calling the getBytes method.
what should I write in the lock (don't know what to write here). Also, I
want to find out using Moniter will be better than lock performace wise.
Thanks.
but since you mentioned that lock
KAMRewriteClassLibrary.getReturnBytes.ReturnData(_sessionDisplay, msg);
> > Hello Everyone,
> >
[quoted text clipped - 41 lines]
> --
> Tom Shelton
Peter Duniho - 09 Oct 2007 06:40 GMT
> Actually I cannot use SomeObject obj = new SomeObject(); or object
> _objLock = new object(); since I am calling a static method in that class
Just because the method is static, that doesn't mean you can't use some
synchronization. Just use a static field to store your locking object.
> [...]
> what should I write in the lock (don't know what to write here). Also, I
> want to find out using Moniter will be better than lock performace wise.
As I said, lock() using Monitor. There should be no difference in
performance.
Pete
Tom Shelton - 09 Oct 2007 19:42 GMT
> Hi,
>
[quoted text clipped - 4 lines]
> classLibrary1.class1.GetBytes();
> that's how I am calling the getBytes method.
public class ClassThatNeedsSync
{
private static readonly lockObject = new object();
....
public byte[] GetBytes()
{
lock (lockObject)
{
return Class1.GetBytes();
}
}
}
Again, pretty basic implementation. Just make sure you use lockObject
anytime you need to sync method calls to the static class.
> what should I write in the lock (don't know what to write here). Also, I
> want to find out using Moniter will be better than lock performace wise.
lock is implemented using Monitor - so it shouldn't make any real
difference.
--
Tom Shelton