473,586 Members | 2,707 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Does [MethodImpl(Meth odImplOptions.S ynchronized)] lock the entire object?

Hello,

I've read conflicting posts about
[MethodImpl(Meth odImplOptions.S ynchronized)]. 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 29542
Seeker <my**********@h otmail.com> wrote:
I've read conflicting posts about
[MethodImpl(Meth odImplOptions.S ynchronized)]. 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. CompilerService s;
using System.Threadin g;

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

[MethodImpl(Meth odImplOptions.S ynchronized)]
void Run()
{
Monitor.Pulse(t his);
}
}

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.co m>
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(Meth odImplOptions.S ynchronized)] 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.co m> wrote in message
news:MP******** *************** *@msnews.micros oft.com...
Seeker <my**********@h otmail.com> wrote:
I've read conflicting posts about
[MethodImpl(Meth odImplOptions.S ynchronized)]. 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. CompilerService s;
using System.Threadin g;

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

[MethodImpl(Meth odImplOptions.S ynchronized)]
void Run()
{
Monitor.Pulse(t his);
}
}

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.co m>
http://www.pobox.com/~skeet
If replying to the group, please do not mail me too

Nov 16 '05 #3
Seeker <my**********@h otmail.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(Meth odImplOptions.S ynchronized)] 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.co m>
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.co m> wrote in message
news:MP******** *************** *@msnews.micros oft.com...
Seeker <my**********@h otmail.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(Meth odImplOptions.S ynchronized)] 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.co m>
http://www.pobox.com/~skeet
If replying to the group, please do not mail me too

Nov 16 '05 #5
Seeker <my**********@h otmail.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.co m>
http://www.pobox.com/~skeet
If replying to the group, please do not mail me too
Nov 16 '05 #6

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

4
1223
by: Dennis M. Marks | last post by:
I changed my home page to use <object> rather that <iframe> for an inset scrolling window. It works on my Mac using IE and Netscape. I would like to know if it works in Opera, AOL, and Mozilla and PC IE and Netscape. Latest versions with javascript on. TIA -- Dennis M. Marks http://www.dcs-chico.com/~denmarks/ Replace domain.invalid with...
4
8044
by: Rich Sienkiewicz | last post by:
Some classes, like Queue and SortedList, have a Synchronized method which gives a thread safe wrapper object for these classes. But the lock() statement does the same thing. Is there any rules as to when to use one and not the other? For instance, if I wanted to remove an item in a SortedList, would it be better to lock() it or do it via the...
2
4813
by: Robert A. van Ginkel | last post by:
I have a strange C# problem. Is the following a bug? Because it should be possible to run unsafe code. How can I accomplish this? And where can I read more about this, because documentation on this is rare. I have a project where i need to use some internal calls. In this project I have 'Allow unsafe code blocks' set to true. And in my...
5
4901
by: norbert.thek | last post by:
Hi Can somebody tell me if I'm right or not The attribute Is like an lock(this) over the Method where i put this Attribute?! If I have two methods like:
2
3553
by: yaron | last post by:
Hi, Does MethodImpl Synchronized attribute is like lock(this) ? i mean that a call to Monitor.PulseAll(this) from a Synchronized method will trigger other thread that call to Monitor.Wait(this) ? Thanks.
17
2506
by: euan_woo | last post by:
Hi, Sometimes my program stops and when I break it I see 2 threads both waiting at a lock statement trying to lock the same object. If I look up the call stack of these threads there aren't any other calls to lock statements so I don't see how it's possible for anything to be locked. Can anyone explain this to me? Thanks
10
2188
by: master | last post by:
As far as I understand, 'lock' is the only option, isn't it? Namely, If I want to declare a 'synchronised' method, I do it this way: class A { void AMethod() { lock (this) { // the method body }
2
1977
by: Tom P. | last post by:
I have a class that ahs several methods. It pulls from a queue so it has to be singleton at that point. The question I have is: does the lock{} statement work across methods? If I have A(), B(), and C() and I do this: A() { lock{ SomeWork(); } }
1
1397
by: andersond | last post by:
Firefox often tells me that one of my objects has no properties; and, I really don't know how to respond. //code line: document.getElementById("nearWater").focus(); //error message: document.getElementById("nearWater") has no properties Can anyone shed some light on what it really wants?
0
7912
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main...
0
8202
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. ...
0
8338
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that...
0
8216
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the...
1
5710
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes...
0
3837
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in...
1
2345
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
1
1449
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
0
1180
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating...

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.