472,121 Members | 1,485 Online
Bytes | Software Development & Data Engineering Community
Post +

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 472,121 software developers and data experts.

Does [MethodImpl(MethodImplOptions.Synchronized)] lock the entire object?

Hello,

I've read conflicting posts about
[MethodImpl(MethodImplOptions.Synchronized)]. Does it or does it not lock
the entire object? In my simple test it appears to block just the method but
I wouldn't exactly call my meager test conclusive...

thanks,
Scott
Nov 16 '05 #1
5 28655
Seeker <my**********@hotmail.com> wrote:
I've read conflicting posts about
[MethodImpl(MethodImplOptions.Synchronized)]. Does it or does it not lock
the entire object? In my simple test it appears to block just the method but
I wouldn't exactly call my meager test conclusive...


It's the equivalent to putting lock(this) round the whole method call.
Here's some code to indicate that:

using System;
using System.Runtime.CompilerServices;
using System.Threading;

class Test
{
static void Main()
{
new Test().Run();
}

[MethodImpl(MethodImplOptions.Synchronized)]
void Run()
{
Monitor.Pulse(this);
}
}

Compile and run it - it's fine. Comment out the attribute and it fails
though, because the thread doesn't own the monitor for "this" without
the attribute.

I wouldn't suggest using the attribute though - I'd use an explicit
lock. I also wouldn't lock on "this" - see
http://www.pobox.com/~skeet/csharp/t...ckchoice.shtml

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet
If replying to the group, please do not mail me too
Nov 16 '05 #2
Nice link, Thanks!

Still toying around with my joke of a test program and I think I discovered
another interesting side effect of this attribute. It seems to not only lock
the current method but also any other methods marked with this attribute in
the object.

MethodA() manipulates dataX
MethodB() manipulates dataX
MethodC() manipulates dataY

Each method has the [MethodImpl(MethodImplOptions.Synchronized)] attribute.
And I start 3 threads (one thread calls MethodA etc). I found that the order
is Thread1, Thread2 then Thread3. So it looks like Thread1 put a lock on all
three methods. If I remove the attribute from Method3 then the order changes
to Thread3, Thread1, Thread2. If I remove all the attributes the order
varies and I can get errors (trying to remove some data that doesn't exist
yet).

I'm starting to understand why this isn't a good thing. I think we are
better off with a more granular locking mechanism using the lock().

Scott

"Jon Skeet [C# MVP]" <sk***@pobox.com> wrote in message
news:MP************************@msnews.microsoft.c om...
Seeker <my**********@hotmail.com> wrote:
I've read conflicting posts about
[MethodImpl(MethodImplOptions.Synchronized)]. Does it or does it not lock the entire object? In my simple test it appears to block just the method but I wouldn't exactly call my meager test conclusive...


It's the equivalent to putting lock(this) round the whole method call.
Here's some code to indicate that:

using System;
using System.Runtime.CompilerServices;
using System.Threading;

class Test
{
static void Main()
{
new Test().Run();
}

[MethodImpl(MethodImplOptions.Synchronized)]
void Run()
{
Monitor.Pulse(this);
}
}

Compile and run it - it's fine. Comment out the attribute and it fails
though, because the thread doesn't own the monitor for "this" without
the attribute.

I wouldn't suggest using the attribute though - I'd use an explicit
lock. I also wouldn't lock on "this" - see
http://www.pobox.com/~skeet/csharp/t...ckchoice.shtml

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet
If replying to the group, please do not mail me too

Nov 16 '05 #3
Seeker <my**********@hotmail.com> wrote:
Nice link, Thanks!

Still toying around with my joke of a test program and I think I discovered
another interesting side effect of this attribute. It seems to not only lock
the current method but also any other methods marked with this attribute in
the object.
Yes, because it's taking out a lock on the "this" reference, which
isn't specific to the method.
MethodA() manipulates dataX
MethodB() manipulates dataX
MethodC() manipulates dataY

Each method has the [MethodImpl(MethodImplOptions.Synchronized)] attribute.
And I start 3 threads (one thread calls MethodA etc). I found that the order
is Thread1, Thread2 then Thread3. So it looks like Thread1 put a lock on all
three methods. If I remove the attribute from Method3 then the order changes
to Thread3, Thread1, Thread2. If I remove all the attributes the order
varies and I can get errors (trying to remove some data that doesn't exist
yet).
You're starting with the wrong concept - the idea of a method being
locked. It's not a method that's locked, it's a reference. In this
case, it's the "this" reference.
I'm starting to understand why this isn't a good thing. I think we are
better off with a more granular locking mechanism using the lock().


Yup - in your case, MethoA and MethodB would use one lock, whereas
MethodC would use another - if you really need that kind of
granularity. (It's often wise to go for a single lock even if it
doesn't give the highest performance, for the sake of memory and ease
of understanding.)

Of course, those methods probably don't need to lock for the *whole*
time they're in the method - just the time that they're manipulating
the shared data.

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet
If replying to the group, please do not mail me too
Nov 16 '05 #4
Ok, just a couple more questions and I really do appreciate your time. When
the attribute is used on some of the methods in an object and you have multi
threads, how is it that a thread is still able to access the non-attribute
methods when there is a lock on "this"? Shouldn't a lock on the object's
reference in effect block all threads and therefore in effect cause the
entire object to lock?

Scott

"Jon Skeet [C# MVP]" <sk***@pobox.com> wrote in message
news:MP************************@msnews.microsoft.c om...
Seeker <my**********@hotmail.com> wrote:
Nice link, Thanks!

Still toying around with my joke of a test program and I think I discovered another interesting side effect of this attribute. It seems to not only lock the current method but also any other methods marked with this attribute in the object.


Yes, because it's taking out a lock on the "this" reference, which
isn't specific to the method.
MethodA() manipulates dataX
MethodB() manipulates dataX
MethodC() manipulates dataY

Each method has the [MethodImpl(MethodImplOptions.Synchronized)] attribute. And I start 3 threads (one thread calls MethodA etc). I found that the order is Thread1, Thread2 then Thread3. So it looks like Thread1 put a lock on all three methods. If I remove the attribute from Method3 then the order changes to Thread3, Thread1, Thread2. If I remove all the attributes the order
varies and I can get errors (trying to remove some data that doesn't exist yet).


You're starting with the wrong concept - the idea of a method being
locked. It's not a method that's locked, it's a reference. In this
case, it's the "this" reference.
I'm starting to understand why this isn't a good thing. I think we are
better off with a more granular locking mechanism using the lock().


Yup - in your case, MethoA and MethodB would use one lock, whereas
MethodC would use another - if you really need that kind of
granularity. (It's often wise to go for a single lock even if it
doesn't give the highest performance, for the sake of memory and ease
of understanding.)

Of course, those methods probably don't need to lock for the *whole*
time they're in the method - just the time that they're manipulating
the shared data.

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet
If replying to the group, please do not mail me too

Nov 16 '05 #5
Seeker <my**********@hotmail.com> wrote:
Ok, just a couple more questions and I really do appreciate your time. When
the attribute is used on some of the methods in an object and you have multi
threads, how is it that a thread is still able to access the non-attribute
methods when there is a lock on "this"? Shouldn't a lock on the object's
reference in effect block all threads and therefore in effect cause the
entire object to lock?


No - the lock only has an effect if another thread tries to acquire the
lock. If a method isn't marked with the attribute and doesn't contain
any lock statements, no locking occurs.

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet
If replying to the group, please do not mail me too
Nov 16 '05 #6

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

4 posts views Thread by Dennis M. Marks | last post: by
4 posts views Thread by Rich Sienkiewicz | last post: by
2 posts views Thread by Robert A. van Ginkel | last post: by
17 posts views Thread by euan_woo | last post: by
2 posts views Thread by Tom P. | last post: by

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.