ptmcomp
asked on
Difference between [MethodImpl(MethodImplOptions.Synchronized)] and lock(this)
I was searching for a C# counterpart for the Java "synchronized" keyword and I found that there is an Attribute "[MethodImpl(MethodImplOpt ions.Synch ronized)]" .
There are quite a lot of synchronization attributes in C# which is rather confusing.
What is the difference between:
public void MyMethod()
{
lock(this)
{
...
}
}
and this:
[MethodImpl(MethodImplOpti ons.Synchr onized)]
public void MyMethod()
{
...
}
There are quite a lot of synchronization attributes in C# which is rather confusing.
What is the difference between:
public void MyMethod()
{
lock(this)
{
...
}
}
and this:
[MethodImpl(MethodImplOpti
public void MyMethod()
{
...
}
SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
if you use the myMethodLockRoot object only for this, yes
ASKER
If I translate this Java code:
public synchronized void myMethod() {
....
}
Shall I use the Attribute or an explicit lock?
PS: @TheAvenger: I'll give you the points but I don't want to close the question yet, I will raise the points and split them if I get more input about differences in IL-Code or other background information.
public synchronized void myMethod() {
....
}
Shall I use the Attribute or an explicit lock?
PS: @TheAvenger: I'll give you the points but I don't want to close the question yet, I will raise the points and split them if I get more input about differences in IL-Code or other background information.
** Not for points **
I think Avenger has pretty much nailed it right on the head. It really comes down to locking an object or a method (more or less).
If the object exists ONLY inside of a single method pretty much either code does the exact same thing.
In your particular case
<Java>
public synchronized void myMethod() {
....
}
You'll want to use the attribute method of synchronizing.
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Can't add anything more to _TAD_: said all
ASKER
Ok guys, thanks a lot!
ASKER
Weird! - How does EE determine which is the "accepted" and which is the "assisted" answer?? Anyway it was meant to be other way around and the points should be 250 for TheAvenger and 100 for _TAD_ both with grade A.
The grade is A and I got 1000 points (4 x 250) so I think only the remarks here are wrong....
ASKER
Just had a look at my Java book it says synchronize locks "this" which would mean:
<JAVA>
public synchronized void myMethod() {
....
}
-- Same as --
<C#>
public void MyMethod()
{
lock(this)
{
...
}
}
Anyway I think I won't use the Attribute since it's harder to understand.
<JAVA>
public synchronized void myMethod() {
....
}
-- Same as --
<C#>
public void MyMethod()
{
lock(this)
{
...
}
}
Anyway I think I won't use the Attribute since it's harder to understand.
TheAvenger,
<quote>
MethodImplOptions.Synchron ized: Specifies the method can be executed by only one thread at a time.
The lock keyword marks a statement block as a critical section by obtaining the mutual-exclusion lock for a given object, executing a statement, and then releasing the lock
So: if you lock an object, all other threads that need to access THIS PARTICULAR OBJECT will wait, until the other object finishes. However if you mark a method as Synchronized, THIS PARTICULAR METHOD will not be executed at more than one thread. Lock secures the object, Synchronized secures the method
</quote>
Incorrect. Both lock(this) and MethodImplOptions.Synchron ized do the same thing - they lock against the object. This is why MethodImplOptions.Synchron ized should be avoided. See http://www.dotnet247.com/247reference/msgs/45/225597.aspx (for example).
Seeya
Matthew
<quote>
MethodImplOptions.Synchron
The lock keyword marks a statement block as a critical section by obtaining the mutual-exclusion lock for a given object, executing a statement, and then releasing the lock
So: if you lock an object, all other threads that need to access THIS PARTICULAR OBJECT will wait, until the other object finishes. However if you mark a method as Synchronized, THIS PARTICULAR METHOD will not be executed at more than one thread. Lock secures the object, Synchronized secures the method
</quote>
Incorrect. Both lock(this) and MethodImplOptions.Synchron
Seeya
Matthew
ASKER
Thanks for the hint. I think I'll stick to the explicit locks since I know what they do and don't change behaviour in further versions.
ASKER
private static object myMethodLockRoot = new object();
public void MyMethod()
{
lock(myMethodLockRoot)
{
...
}
}