Synchronization Attributes

C

CKKwan

Dear All,

[Synchronization]
Can synchronize a class, any function is called and the entire class
is locked.

[MethodImplAttribute(MethodImplOptions.Synchronized)]
Can synchronize a method

What if I need to Lock a class only when specific method is call?

For Example:

public class C
{
public void A() {}
public void B() {}
}

The instance of class C need to be locked when method B() is called,
but not lock when method A() is called.

Certainly I can manually put 'lock(this)' in method B(). But is there
any other way of doing this?

Thanks in advance
 
S

sturyuu5eye

[MethodImplAttribute(MethodImplOptions.Synchronized)]
Can synchronize a method

No, not precisely.  Synchronizes _all_ such labeled methods in a class, 
along with anything that explicitly uses the type (when applied to static 
members) or instance (when applied to instance members) for locking.

Oh Pete, you are correct! It lock on the instance. And that is exactly
what I want.

Thanks for the help and sorry for the confusion.

p.s. I wonder why google forced me to use this stupid Id on reply :(
 

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