What does MethodImplOptions.Synchronized do?

.NetMultithreading

.Net Problem Overview


What does MethodImplOptions.Synchronized do?

Is the code below

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

equivalent to

public void Method()
{
    lock(this)
    {
        MethodImpl();
    }
}

.Net Solutions


Solution 1 - .Net

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.

Solution 2 - .Net

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

Solution 3 - .Net

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionAmitabhView Question on Stackoverflow
Solution 1 - .NetDavid BasarabView Answer on Stackoverflow
Solution 2 - .NetRico SuterView Answer on Stackoverflow
Solution 3 - .NetMichael StollView Answer on Stackoverflow