Go Premium for a chance to win a PS4. Enter to Win

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 7634
  • Last Modified:

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(MethodImplOptions.Synchronized)]".
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(MethodImplOptions.Synchronized)]
public void MyMethod()
{
    ...
}
0
ptmcomp
Asked:
ptmcomp
  • 6
  • 4
  • 2
  • +1
2 Solutions
 
TheAvengerCommented:
MethodImplOptions.Synchronized: 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
0
 
ptmcompAuthor Commented:
Ok so [MethodImpl(MethodImplOptions.Synchronized)] does the same as the code below?

private static object myMethodLockRoot = new object();
public void MyMethod()
{
   lock(myMethodLockRoot)
   {
       ...
   }
}
0
 
TheAvengerCommented:
if you use the myMethodLockRoot  object only for this, yes
0
What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

 
ptmcompAuthor Commented:
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.
0
 
_TAD_Commented:


** 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.
0
 
_TAD_Commented:


again....

** Not for Points **


My Java is a bit rusty, but here goes....


<JAVA>
public synchronized void myMethod() {
    ....
 }

                         -- Same as --

<C#>
[MethodImpl(MethodImplOptions.Synchronized)]
public void MyMethod()
{
    ...
}





<JAVA>
public void myMethod() {
    StringWriter swr = new StringWriter(<Params>);
    Synchronized(swr){
        ...
    }
 }

            --- Same As ---

<C#>
public void myMethod() {
    StreamWriter swr = new StreamWriter(<params>);
    lock(swr){
        ...
    }
 }
0
 
TheAvengerCommented:
Can't add anything more to _TAD_: said all
0
 
ptmcompAuthor Commented:
Ok guys, thanks a lot!
0
 
ptmcompAuthor Commented:
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.
0
 
TheAvengerCommented:
The grade is A and I got 1000 points (4 x 250) so I think only the remarks here are wrong....
0
 
ptmcompAuthor Commented:
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.
0
 
mjwillsCommented:
TheAvenger,

<quote>
MethodImplOptions.Synchronized: 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.Synchronized do the same thing - they lock against the object. This is why MethodImplOptions.Synchronized should be avoided. See http://www.dotnet247.com/247reference/msgs/45/225597.aspx (for example).

Seeya
Matthew
0
 
ptmcompAuthor Commented:
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.
0

Featured Post

Free Tool: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

  • 6
  • 4
  • 2
  • +1
Tackle projects and never again get stuck behind a technical roadblock.
Join Now