Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. Other Discussions
  3. The Weird and The Wonderful
  4. Locking horror: Am I going mad?

Locking horror: Am I going mad?

Scheduled Pinned Locked Moved The Weird and The Wonderful
question
7 Posts 6 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • E Offline
    E Offline
    ewan
    wrote on last edited by
    #1

    In a new body of code I've "inherited" there is a lot of code that looks something like this (simplified somewhat):

    public class A
    {
    private ArrayList list = new ArrayList();
    private object m_lock = new object();

    public A()
    {
    lock(m_lock)
    {
    list.Add("MyString");
    }
    }
    }

    Now am I going mad? That lock statement to my eye is marginally pointless. Is there ever any justification for locking on a non static, private object in a constructor? Cheers, E.

    P CPalliniC D C 4 Replies Last reply
    0
    • E ewan

      In a new body of code I've "inherited" there is a lot of code that looks something like this (simplified somewhat):

      public class A
      {
      private ArrayList list = new ArrayList();
      private object m_lock = new object();

      public A()
      {
      lock(m_lock)
      {
      list.Add("MyString");
      }
      }
      }

      Now am I going mad? That lock statement to my eye is marginally pointless. Is there ever any justification for locking on a non static, private object in a constructor? Cheers, E.

      P Offline
      P Offline
      PIEBALDconsult
      wrote on last edited by
      #2

      Probably not, and why have a separate object for the lock either (in the included code anyway)? But it could be worse as well.

      A 1 Reply Last reply
      0
      • E ewan

        In a new body of code I've "inherited" there is a lot of code that looks something like this (simplified somewhat):

        public class A
        {
        private ArrayList list = new ArrayList();
        private object m_lock = new object();

        public A()
        {
        lock(m_lock)
        {
        list.Add("MyString");
        }
        }
        }

        Now am I going mad? That lock statement to my eye is marginally pointless. Is there ever any justification for locking on a non static, private object in a constructor? Cheers, E.

        CPalliniC Offline
        CPalliniC Offline
        CPallini
        wrote on last edited by
        #3

        The previous position of the developer was in jeweller’s shop. :-D

        If the Lord God Almighty had consulted me before embarking upon the Creation, I would have recommended something simpler. -- Alfonso the Wise, 13th Century King of Castile.
        This is going on my arrogant assumptions. You may have a superb reason why I'm completely wrong. -- Iain Clarke
        [My articles]

        In testa che avete, signor di Ceprano?

        1 Reply Last reply
        0
        • E ewan

          In a new body of code I've "inherited" there is a lot of code that looks something like this (simplified somewhat):

          public class A
          {
          private ArrayList list = new ArrayList();
          private object m_lock = new object();

          public A()
          {
          lock(m_lock)
          {
          list.Add("MyString");
          }
          }
          }

          Now am I going mad? That lock statement to my eye is marginally pointless. Is there ever any justification for locking on a non static, private object in a constructor? Cheers, E.

          D Offline
          D Offline
          Dust Signs
          wrote on last edited by
          #4

          The bottom-most MSDN example for the lock statement actually looks quite similar: http://msdn.microsoft.com/en-us/library/c5kehkcz(VS.80).aspx[^] Regards, Dust Signs

          P 1 Reply Last reply
          0
          • D Dust Signs

            The bottom-most MSDN example for the lock statement actually looks quite similar: http://msdn.microsoft.com/en-us/library/c5kehkcz(VS.80).aspx[^] Regards, Dust Signs

            P Offline
            P Offline
            PIEBALDconsult
            wrote on last edited by
            #5

            Yet not at all the same.

            1 Reply Last reply
            0
            • P PIEBALDconsult

              Probably not, and why have a separate object for the lock either (in the included code anyway)? But it could be worse as well.

              A Offline
              A Offline
              Andrew Rissing
              wrote on last edited by
              #6

              [I'll get there, just give me a sec. :D] From a coding perspective, people often use 'this' as the object to lock on, but that is not a preferred method of locking. The reason is that code outside of the class might want to use that object to lock on as well, there by possibly causing a deadlock. So, the recommendation is to often use internal private objects to do this. Now, in this case, I would assume the array list would be the best item to lock on. But out of readability or perhaps if that array list is reinitialized later, they may have opted for a separate unchanging object reference for the lock to prevent any kind of concurrency issues. So, in short, the lock in the constructor is needless, but the object used in the lock very well could be needed depending on the implementation of the class.

              1 Reply Last reply
              0
              • E ewan

                In a new body of code I've "inherited" there is a lot of code that looks something like this (simplified somewhat):

                public class A
                {
                private ArrayList list = new ArrayList();
                private object m_lock = new object();

                public A()
                {
                lock(m_lock)
                {
                list.Add("MyString");
                }
                }
                }

                Now am I going mad? That lock statement to my eye is marginally pointless. Is there ever any justification for locking on a non static, private object in a constructor? Cheers, E.

                C Offline
                C Offline
                cliran
                wrote on last edited by
                #7

                probably can cut the guy a little slack, at least he didn't locked on 'this' or 'typeof'..

                1 Reply Last reply
                0
                Reply
                • Reply as topic
                Log in to reply
                • Oldest to Newest
                • Newest to Oldest
                • Most Votes


                • Login

                • Don't have an account? Register

                • Login or register to search.
                • First post
                  Last post
                0
                • Categories
                • Recent
                • Tags
                • Popular
                • World
                • Users
                • Groups