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. General Programming
  3. C#
  4. Threadsafe property

Threadsafe property

Scheduled Pinned Locked Moved C#
question
8 Posts 4 Posters 1 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.
  • P Offline
    P Offline
    Philip F
    wrote on last edited by
    #1

    Hi everybody! just wanted to ask a short question: I have a property, that should be (externally) threadsafe. Is this good coding style, or do you have a better solution?

    class Manager
    {
    private object locker = new object();

    private MyType threadSafeProperty = new MyType();
    public MyType ThreadSafeProperty
    {
    	get { lock (locker) { return threadSafeProperty; } }
               set { lock (locker) { threadSafeProperty = value; } }
    }
    

    }

    Thank you, Phil

    I won’t not use no double negatives.

    R N L 3 Replies Last reply
    0
    • P Philip F

      Hi everybody! just wanted to ask a short question: I have a property, that should be (externally) threadsafe. Is this good coding style, or do you have a better solution?

      class Manager
      {
      private object locker = new object();

      private MyType threadSafeProperty = new MyType();
      public MyType ThreadSafeProperty
      {
      	get { lock (locker) { return threadSafeProperty; } }
                 set { lock (locker) { threadSafeProperty = value; } }
      }
      

      }

      Thank you, Phil

      I won’t not use no double negatives.

      R Offline
      R Offline
      Roger Alsing 0
      wrote on last edited by
      #2

      AFAIK, "lock" is not recomended since it can result in permanent lockups of an object for ever. Monitor.TryEnter does the same but takes an argument specifying timeout interval. Also, the locker object should be readonly.

      My Blog

      1 Reply Last reply
      0
      • P Philip F

        Hi everybody! just wanted to ask a short question: I have a property, that should be (externally) threadsafe. Is this good coding style, or do you have a better solution?

        class Manager
        {
        private object locker = new object();

        private MyType threadSafeProperty = new MyType();
        public MyType ThreadSafeProperty
        {
        	get { lock (locker) { return threadSafeProperty; } }
                   set { lock (locker) { threadSafeProperty = value; } }
        }
        

        }

        Thank you, Phil

        I won’t not use no double negatives.

        N Offline
        N Offline
        Nicholas Butler
        wrote on last edited by
        #3

        This will not work if your intent is to synchronize access to the MyType instance. You are only holding the lock while getting the reference to the instance. You then return the instance to the calling code which may then use it without synchronization. The solution depends on what the calling code does with the MyType instance. You could expose a synchronization object in the Manager class, but that is open to abuse. Or you could add methods for manipulating the instance through the Manager class and use locks in those methods. YMMV. Nick

        ---------------------------------- Be excellent to each other :)

        P 1 Reply Last reply
        0
        • P Philip F

          Hi everybody! just wanted to ask a short question: I have a property, that should be (externally) threadsafe. Is this good coding style, or do you have a better solution?

          class Manager
          {
          private object locker = new object();

          private MyType threadSafeProperty = new MyType();
          public MyType ThreadSafeProperty
          {
          	get { lock (locker) { return threadSafeProperty; } }
                     set { lock (locker) { threadSafeProperty = value; } }
          }
          

          }

          Thank you, Phil

          I won’t not use no double negatives.

          L Offline
          L Offline
          led mike
          wrote on last edited by
          #4

          Philip. Your concept of locking an object while returning a reference to it as a means of synchronization indicates you are far from understanding the fundamental concepts of synchronization. I strongly urge you to study the subject more before attempting any implementation requiring thread synchronization.

          P 1 Reply Last reply
          0
          • N Nicholas Butler

            This will not work if your intent is to synchronize access to the MyType instance. You are only holding the lock while getting the reference to the instance. You then return the instance to the calling code which may then use it without synchronization. The solution depends on what the calling code does with the MyType instance. You could expose a synchronization object in the Manager class, but that is open to abuse. Or you could add methods for manipulating the instance through the Manager class and use locks in those methods. YMMV. Nick

            ---------------------------------- Be excellent to each other :)

            P Offline
            P Offline
            Philip F
            wrote on last edited by
            #5

            Thank you for you answer Nick. That was actually why asked this, I had a bad feeling about that ;) MyType is some kind of generic List. I wanted to ensure that I had exclusive access to the list as long as I was manipulating it. Stupid idea now ;) I will expose the lock and acquire it as long as I am manipulating the list. Phil

            I won’t not use no double negatives.

            N 1 Reply Last reply
            0
            • L led mike

              Philip. Your concept of locking an object while returning a reference to it as a means of synchronization indicates you are far from understanding the fundamental concepts of synchronization. I strongly urge you to study the subject more before attempting any implementation requiring thread synchronization.

              P Offline
              P Offline
              Philip F
              wrote on last edited by
              #6

              I'll do that. :)

              I won’t not use no double negatives.

              L 1 Reply Last reply
              0
              • P Philip F

                Thank you for you answer Nick. That was actually why asked this, I had a bad feeling about that ;) MyType is some kind of generic List. I wanted to ensure that I had exclusive access to the list as long as I was manipulating it. Stupid idea now ;) I will expose the lock and acquire it as long as I am manipulating the list. Phil

                I won’t not use no double negatives.

                N Offline
                N Offline
                Nicholas Butler
                wrote on last edited by
                #7

                No problem. You might like to read http://www.albahari.com/threading/[^] :) Nick

                ---------------------------------- Be excellent to each other :)

                1 Reply Last reply
                0
                • P Philip F

                  I'll do that. :)

                  I won’t not use no double negatives.

                  L Offline
                  L Offline
                  led mike
                  wrote on last edited by
                  #8

                  Philip F. wrote:

                  I'll do that.

                  Then you will do well. If it helps you, back when I started trying to understand mulit-threading I found Jeffery Richters book Advanced Windows[^] extremely helpful. Keep in mind it was the first edition so no guarantees the new one is the same.

                  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