mutual exclusive lock

G

Guest

Hello Everyone,

I need to run a peice of code without any interruptionsby other thread.
In other words I want to put a mutual exclusion lock.

There is a method that I need to call within that lock. This method resides
in a class that in turn resides in a classLibrary. How can I call a monitor
or lock method on the class.

So for example I have a class Library called classLibrary1. I have class
inside the callsLibrary1 called Class1. I need to call a method called
getBytes() from class1.
This method getBytes() in turn calls other methods in the same class and
different classes.

How can I put a mutual exclusive lock until I am done calling this method
and get the return value from the getBytes().

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.



lock(not sure what to call)

or Monitor(not sure what to call)

Any help will be apprecaited.

Thanks.
 
P

Peter Duniho

Vinki said:
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
 
T

Tom Shelton

Hello Everyone,

I need to run a peice of code without any interruptionsby other thread.
In other words I want to put a mutual exclusion lock.

There is a method that I need to call within that lock. This method resides
in a class that in turn resides in a classLibrary. How can I call a monitor
or lock method on the class.

So for example I have a class Library called classLibrary1. I have class
inside the callsLibrary1 called Class1. I need to call a method called
getBytes() from class1.
This method getBytes() in turn calls other methods in the same class and
different classes.

How can I put a mutual exclusive lock until I am done calling this method
and get the return value from the getBytes().

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.

lock(not sure what to call)

or Monitor(not sure what to call)

Any help will be apprecaited.

Thanks.

Are you looking for something like:

SomeObject obj = new SomeObject();
byte[] bytes = null;

lock (obj)
{
bytes = obj.GetBytes();
}

// do cool stuff
 
G

Guest

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);

Tom Shelton said:
Hello Everyone,

I need to run a peice of code without any interruptionsby other thread.
In other words I want to put a mutual exclusion lock.

There is a method that I need to call within that lock. This method resides
in a class that in turn resides in a classLibrary. How can I call a monitor
or lock method on the class.

So for example I have a class Library called classLibrary1. I have class
inside the callsLibrary1 called Class1. I need to call a method called
getBytes() from class1.
This method getBytes() in turn calls other methods in the same class and
different classes.

How can I put a mutual exclusive lock until I am done calling this method
and get the return value from the getBytes().

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.

lock(not sure what to call)

or Monitor(not sure what to call)

Any help will be apprecaited.

Thanks.

Are you looking for something like:

SomeObject obj = new SomeObject();
byte[] bytes = null;

lock (obj)
{
bytes = obj.GetBytes();
}

// do cool stuff
 
P

Peter Duniho

Vinki said:
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
 
T

Tom Shelton

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.

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.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top