Locking in C#

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Akula

    #1

    Locking in C#

    Does anyone know whether or not it is faster to lock a simple object,
    rather than a complex type?

    For example:

    Dictionary<stri ng, SomeOtherClassd ict = new Dictionary<stri ng,
    SomeOtherClass> ();
    object lockObject = new object();

    --- Example 1 ---

    lock (dict)
    {
    // Do something with dict
    }

    --- Example 2 ---

    lock (lockObject)
    {
    // Do something with dict
    }

    I have seen a improvment in performance in the simple object locking
    v.s. locking the entire dictionary, but what are the side affects?
    And why am I seeing that improvment in the first place?

    Thanks
  • Nicholas Paldino [.NET/C# MVP]

    #2
    Re: Locking in C#

    Akula,

    I doubt you have seen a performance improvement, as what is actually
    being locked on is an internal member of the object which has nothing to do
    with anything else in the object (it's type, members, etc, etc).

    If you are seeing a difference, I would wager that it is in the cost of
    constructing the different objects, and not actually locking on them.


    --
    - Nicholas Paldino [.NET/C# MVP]
    - mvp@spam.guard. caspershouse.co m

    "Akula" <aronweiler@gma il.comwrote in message
    news:842968b0-5d14-4afc-abe1-a297e2d885c0@b4 0g2000prf.googl egroups.com...
    Does anyone know whether or not it is faster to lock a simple object,
    rather than a complex type?
    >
    For example:
    >
    Dictionary<stri ng, SomeOtherClassd ict = new Dictionary<stri ng,
    SomeOtherClass> ();
    object lockObject = new object();
    >
    --- Example 1 ---
    >
    lock (dict)
    {
    // Do something with dict
    }
    >
    --- Example 2 ---
    >
    lock (lockObject)
    {
    // Do something with dict
    }
    >
    I have seen a improvment in performance in the simple object locking
    v.s. locking the entire dictionary, but what are the side affects?
    And why am I seeing that improvment in the first place?
    >
    Thanks

    Comment

    • Jon Skeet [C# MVP]

      #3
      Re: Locking in C#

      Akula <aronweiler@gma il.comwrote:
      Does anyone know whether or not it is faster to lock a simple object,
      rather than a complex type?
      You don't lock an object - you lock a reference. The locking itself
      doesn't really interact with the object.

      <snip>
      I have seen a improvment in performance in the simple object locking
      v.s. locking the entire dictionary, but what are the side affects?
      And why am I seeing that improvment in the first place?
      I suspect you're seeing the improvement due to flaws in your
      benchmarking methodology, although it's hard to say without seeing the
      code.

      However, I personally almost always use a separate object to provide
      the reference for locking, as this means I can ensure it's never made
      publically available, which helps to avoid threading issues.

      --
      Jon Skeet - <skeet@pobox.co m>
      http://www.pobox.com/~skeet Blog: http://www.msmvps.com/jon.skeet
      If replying to the group, please do not mail me too

      Comment

      • Akula

        #4
        Re: Locking in C#

        Interesting... but there is a definite performance increase when I
        merely switch the object being locked (from a class, to a simple
        object).

        Is there a resource on the web that describes, in detail, the
        internals of the lock statement? I have looked at my complied code
        using ildasm, and like you said, there appears to be no difference in
        how it locks.

        Comment

        • Jon Skeet [C# MVP]

          #5
          Re: Locking in C#

          Akula <aronweiler@gma il.comwrote:
          Interesting... but there is a definite performance increase when I
          merely switch the object being locked (from a class, to a simple
          object).
          Could you post a short but complete program which demonstrates this?

          See http://www.pobox.com/~skeet/csharp/complete.html for details of
          what I mean by that.
          Is there a resource on the web that describes, in detail, the
          internals of the lock statement? I have looked at my complied code
          using ildasm, and like you said, there appears to be no difference in
          how it locks.
          Lock just compiles into pair of calls to Monitor.Enter/Exit with
          try/finally.

          --
          Jon Skeet - <skeet@pobox.co m>
          http://www.pobox.com/~skeet Blog: http://www.msmvps.com/jon.skeet
          If replying to the group, please do not mail me too

          Comment

          • Akula

            #6
            Re: Locking in C#

            Ok, so after some more investigation, and downloading some new
            profiling tools, I have determined that it was the profiler that I was
            using.

            So, those of you who were suggesting that the metrics I was gathering
            were wrong are correct.

            Thanks for the help, all.

            Comment

            • Scott Gifford

              #7
              Re: Locking in C#

              Akula <aronweiler@gma il.comwrites:
              Interesting... but there is a definite performance increase when I
              merely switch the object being locked (from a class, to a simple
              object).
              >
              Is there a resource on the web that describes, in detail, the
              internals of the lock statement? I have looked at my complied code
              using ildasm, and like you said, there appears to be no difference in
              how it locks.
              I found Jon's article here very helpful in understanding how C#
              locking works:



              ----Scott.

              Comment

              Working...