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. The Lounge
  3. What's great about C#

What's great about C#

Scheduled Pinned Locked Moved The Lounge
csharphtmlbusiness
13 Posts 5 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.
  • P Offline
    P Offline
    peterchen
    wrote on last edited by
    #1

    The lock statement, dude. Writing Multithreaded code never was so much fun.


    "Der Geist des Kriegers ist erwacht / Ich hab die Macht" StS
    sighist | Agile Programming | doxygen

    W G 2 Replies Last reply
    0
    • P peterchen

      The lock statement, dude. Writing Multithreaded code never was so much fun.


      "Der Geist des Kriegers ist erwacht / Ich hab die Macht" StS
      sighist | Agile Programming | doxygen

      W Offline
      W Offline
      wayward
      wrote on last edited by
      #2

      As long as its not "lock (typeof(Foo))"... James.

      P D 2 Replies Last reply
      0
      • W wayward

        As long as its not "lock (typeof(Foo))"... James.

        P Offline
        P Offline
        peterchen
        wrote on last edited by
        #3

        ?? :~


        "Der Geist des Kriegers ist erwacht / Ich hab die Macht" StS
        sighist | Agile Programming | doxygen

        W 1 Reply Last reply
        0
        • W wayward

          As long as its not "lock (typeof(Foo))"... James.

          D Offline
          D Offline
          Daniel Turini
          wrote on last edited by
          #4

          wayward wrote: As long as its not "lock (typeof(Foo))"... And as long as it's not lock (string.Empty) And as long as it's not lock (this) And as long as it's not lock (someStringConstant) and you use string pooling (Release Build) and you happen to have two equal string constants on your code (you'll lock the same object). Actually, I found that the most safe and reliable object to lock is always some string constant which have each a different value, a unique GUID.

          // Quantum sort algorithm implementation
          while (!sorted)
          ;

          J P W 3 Replies Last reply
          0
          • D Daniel Turini

            wayward wrote: As long as its not "lock (typeof(Foo))"... And as long as it's not lock (string.Empty) And as long as it's not lock (this) And as long as it's not lock (someStringConstant) and you use string pooling (Release Build) and you happen to have two equal string constants on your code (you'll lock the same object). Actually, I found that the most safe and reliable object to lock is always some string constant which have each a different value, a unique GUID.

            // Quantum sort algorithm implementation
            while (!sorted)
            ;

            J Offline
            J Offline
            Jorgen Sigvardsson
            wrote on last edited by
            #5

            Hmm.. how can these points be any problems? lock is surely aware of the calling thread, and will thus not block the thread who acquired the lock?? -- Gnnnnmmmpppppppfffffhhh!

            1 Reply Last reply
            0
            • P peterchen

              The lock statement, dude. Writing Multithreaded code never was so much fun.


              "Der Geist des Kriegers ist erwacht / Ich hab die Macht" StS
              sighist | Agile Programming | doxygen

              G Offline
              G Offline
              Giles
              wrote on last edited by
              #6

              Yep, for me when I first used it was the static constructor. Makes it easier to write clean code.


              "Je pense, donc je mange." - Rene Descartes 1689 - Just before his mother put his tea on the table. Shameless Plug - Distributed Database Transactions in .NET using COM+

              1 Reply Last reply
              0
              • D Daniel Turini

                wayward wrote: As long as its not "lock (typeof(Foo))"... And as long as it's not lock (string.Empty) And as long as it's not lock (this) And as long as it's not lock (someStringConstant) and you use string pooling (Release Build) and you happen to have two equal string constants on your code (you'll lock the same object). Actually, I found that the most safe and reliable object to lock is always some string constant which have each a different value, a unique GUID.

                // Quantum sort algorithm implementation
                while (!sorted)
                ;

                P Offline
                P Offline
                peterchen
                wrote on last edited by
                #7

                a) you can even lock on string.Empty?? who would want to do that? b) what#s so bad about lock(this) and lock(typeof(foo)) ?


                "Der Geist des Kriegers ist erwacht / Ich hab die Macht" StS
                sighist | Agile Programming | doxygen

                D 1 Reply Last reply
                0
                • P peterchen

                  ?? :~


                  "Der Geist des Kriegers ist erwacht / Ich hab die Macht" StS
                  sighist | Agile Programming | doxygen

                  W Offline
                  W Offline
                  wayward
                  wrote on last edited by
                  #8

                  There is an article on the Microsoft website about how this is a really bad thing to do as it can cause all sorts of problems within the .NET runtime - including the inability to create new objects of that type whilst the lock is being held. I know its in the examples, but they say they will be revising them in the future... James.

                  1 Reply Last reply
                  0
                  • D Daniel Turini

                    wayward wrote: As long as its not "lock (typeof(Foo))"... And as long as it's not lock (string.Empty) And as long as it's not lock (this) And as long as it's not lock (someStringConstant) and you use string pooling (Release Build) and you happen to have two equal string constants on your code (you'll lock the same object). Actually, I found that the most safe and reliable object to lock is always some string constant which have each a different value, a unique GUID.

                    // Quantum sort algorithm implementation
                    while (!sorted)
                    ;

                    W Offline
                    W Offline
                    wayward
                    wrote on last edited by
                    #9

                    Actually, there is a very good use of "lock(stringConst)" - when you have a multithreaded cache and only want to lock the portion on the cache (or key) you are about to update... James.

                    D 1 Reply Last reply
                    0
                    • P peterchen

                      a) you can even lock on string.Empty?? who would want to do that? b) what#s so bad about lock(this) and lock(typeof(foo)) ?


                      "Der Geist des Kriegers ist erwacht / Ich hab die Macht" StS
                      sighist | Agile Programming | doxygen

                      D Offline
                      D Offline
                      Daniel Turini
                      wrote on last edited by
                      #10

                      peterchen wrote: you can even lock on string.Empty?? who would want to do that? Unfortunately, I see lots of people doing this. peterchen wrote: what#s so bad about lock(this) Your object cannot be used for a lock, or you'll risk a deadlock. Instead of doing it, create a private field and use lock(privateField). peterchen wrote: and lock(typeof(foo)) ? Because it's the same as lock(foo.GetType()), which happens to return a cached value, so you again risk a deadlock. "Oh, I know what I am doing" - Yes, but the one who consumes your class sometimes don't. The rule is: always lock on a private field or a private static field, and if it is a string, make sure it is unique on the entire assembly.

                      // Quantum sort algorithm implementation
                      while (!sorted)
                      ;

                      P 1 Reply Last reply
                      0
                      • D Daniel Turini

                        peterchen wrote: you can even lock on string.Empty?? who would want to do that? Unfortunately, I see lots of people doing this. peterchen wrote: what#s so bad about lock(this) Your object cannot be used for a lock, or you'll risk a deadlock. Instead of doing it, create a private field and use lock(privateField). peterchen wrote: and lock(typeof(foo)) ? Because it's the same as lock(foo.GetType()), which happens to return a cached value, so you again risk a deadlock. "Oh, I know what I am doing" - Yes, but the one who consumes your class sometimes don't. The rule is: always lock on a private field or a private static field, and if it is a string, make sure it is unique on the entire assembly.

                        // Quantum sort algorithm implementation
                        while (!sorted)
                        ;

                        P Offline
                        P Offline
                        peterchen
                        wrote on last edited by
                        #11

                        Daniel Turini wrote: or you'll risk a deadlock why? (link would be enough)


                        "Der Geist des Kriegers ist erwacht / Ich hab die Macht" StS
                        sighist | Agile Programming | doxygen

                        D 1 Reply Last reply
                        0
                        • W wayward

                          Actually, there is a very good use of "lock(stringConst)" - when you have a multithreaded cache and only want to lock the portion on the cache (or key) you are about to update... James.

                          D Offline
                          D Offline
                          Daniel Turini
                          wrote on last edited by
                          #12

                          wayward wrote: Actually, there is a very good use of "lock(stringConst)" - when you have a multithreaded cache and only want to lock the portion on the cache (or key) you are about to update... I think you misunderstood: This is good practice, indeed. Just make sure the stringConst contains a unique string or you may face a deadlock - only on Release builds.

                          // Quantum sort algorithm implementation
                          while (!sorted)
                          ;

                          1 Reply Last reply
                          0
                          • P peterchen

                            Daniel Turini wrote: or you'll risk a deadlock why? (link would be enough)


                            "Der Geist des Kriegers ist erwacht / Ich hab die Macht" StS
                            sighist | Agile Programming | doxygen

                            D Offline
                            D Offline
                            Daniel Turini
                            wrote on last edited by
                            #13

                            peterchen wrote: why? (link would be enough) I have no link to this. Just as a sample, imagine a class A & B where class A.someMethod(B b) does:

                            lock(this)
                            {
                            b.SomeMethod();
                            }

                            And b.SomeMethod() does:

                            lock(this)
                            {
                            ....
                            }

                            Reasonable, huh? This will work, until someone who calls it, do:

                            B b = new B();
                            lock (B)
                            {
                            }

                            In another thread and use the same b to A.someMethod(). Believe me, this really happens in the real world and it's hard to find it. You can avoid all of this hassle with two very simple rules: 1. Always lock your own private fields (static or not, depending on the case). 2. If you lock strings, always use unique strings.

                            // Quantum sort algorithm implementation
                            while (!sorted)
                            ;

                            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