Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What does MethodImplOptions.Synchronized do?

What does MethodImplOptions.Synchronized do?

Is the code below

[MethodImpl(MethodImplOptions.Synchronized)]
public void Method()
{
    MethodImpl();
}

equivalent to

public void Method()
{
    lock(this)
    {
        MethodImpl();
    }
}
like image 816
Amitabh Avatar asked Feb 08 '10 17:02

Amitabh


3 Answers

This was answered by Mr. Jon Skeet on another site.

Quote from Post

It's the equivalent to putting lock(this) round the whole method call.

The post has more example code.

like image 111
David Basarab Avatar answered Oct 16 '22 04:10

David Basarab


For static methods it's the same as:

public class MyClass
{
    public static void Method()
    {
        lock(typeof(MyClass))
        {
           MethodImpl();
        }
    }
}

http://social.msdn.microsoft.com/Forums/en-US/b6a72e00-d4cc-4f29-a6a0-b27551f78b9b/methodimploptionssynchronized-vs-lock

like image 8
Rico Suter Avatar answered Oct 16 '22 03:10

Rico Suter


Yes it is. See MethodImplOptions Enumeration

like image 5
Michael Stoll Avatar answered Oct 16 '22 03:10

Michael Stoll