Need clarifikation about lock(this) and [MethodImpl(MethodImplOptions.Synchronized)]

N

norbert.thek

Hi

Can somebody tell me if I'm right or not

The attribute [MethodImpl(MethodImplOptions.Synchronized)]
Is like an lock(this) over the Method where i put this Attribute?!

If I have two methods like:

[MethodImpl(MethodImplOptions.Synchronized)]
void method1() {do something;}
[MethodImpl(MethodImplOptions.Synchronized)]
void method2() {do something else;}

And I call method1 and while it is still running method2.
The processing of method2 will only start after method1 is finished!

I hope I understand this right
(because if not I will get a serious synchronizing Problem :-/ )


regards
Norbert
 
N

Nicholas Paldino [.NET/C# MVP]

Norbert,

You are right, when you add the MethodImpl attribute with the
synchronized option, it would be the same as locking on the instance of the
object (if it is an instance method) and locking on the Type instance for
that type if it is a static method.

Hope this helps.
 
J

Jon Skeet [C# MVP]

Can somebody tell me if I'm right or not

The attribute [MethodImpl(MethodImplOptions.Synchronized)]
Is like an lock(this) over the Method where i put this Attribute?!

If I have two methods like:

[MethodImpl(MethodImplOptions.Synchronized)]
void method1() {do something;}
[MethodImpl(MethodImplOptions.Synchronized)]
void method2() {do something else;}

And I call method1 and while it is still running method2.
The processing of method2 will only start after method1 is finished!

I hope I understand this right
(because if not I will get a serious synchronizing Problem :-/ )

Yes, that's right. It's rarely a good idea though:

1) It's rare to find a method where the whole method needs to be
synchronized rather than just a portion of it.

2) Locking on "this" or the type object isn't a good idea:
http://www.pobox.com/~skeet/csharp/threads/lockchoice.shtml

3) The attribute is easier to overlook than the much more common lock
statement.
 
N

nobs

The reason i need it is the following

i have a class encapsulate communication over tcp

there are several commandos supported
like
start
stop
getvalue(valueID) and so on

the problem is, that because of synchronisation issues i only want one
command
to be run at one time
(i know its dirty, i want to redesign it next week, but i have a
deadline this week :-( )

nevertheless
thanks for your help
 
J

Jon Skeet [C# MVP]

nobs said:
The reason i need it is the following

i have a class encapsulate communication over tcp

there are several commandos supported
like
start
stop
getvalue(valueID) and so on

the problem is, that because of synchronisation issues i only want one
command
to be run at one time
(i know its dirty, i want to redesign it next week, but i have a
deadline this week :-( )

That just shows you need synchronization - it doesn't show that you
need to use MethodImplOptions.Synchronized.
 

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