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. How to use lock in C#

How to use lock in C#

Scheduled Pinned Locked Moved C#
csharptutorialquestion
12 Posts 4 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.
  • B Bob_Sun

    I have written a multithread program using lock(this) to realize sync. I hung up the other day and i was told lock(this) is not a good way. Can anyone give me some advice and tell my why? Thank you!

    G Offline
    G Offline
    Guffa
    wrote on last edited by
    #3

    What was wrong with the question you posted only half an hour before? Don't spam the forum. --- b { font-weight: normal; }

    B 1 Reply Last reply
    0
    • B Bob_Sun

      I have written a multithread program using lock(this) to realize sync. I hung up the other day and i was told lock(this) is not a good way. Can anyone give me some advice and tell my why? Thank you!

      S Offline
      S Offline
      Steve Hansen
      wrote on last edited by
      #4

      lock(this) can bring you in a deadlock instead create a field like this: private object syncRoot = new object(); and use lock(syncRoot) [EDIT] Not static :) field will be static only for static methods

      B 1 Reply Last reply
      0
      • G Guffa

        What was wrong with the question you posted only half an hour before? Don't spam the forum. --- b { font-weight: normal; }

        B Offline
        B Offline
        Bob_Sun
        wrote on last edited by
        #5

        Soory for made a mistake to post 2 messages. I think it is better to use this thread only

        G 1 Reply Last reply
        0
        • L leppie

          Bob_Sun wrote:

          i was told lock(this) is not a good way.

          And why didnt he back up his claim? It all depends what you need lock'd, but more important where the locking takes place.

          xacc.ide-0.1.3.2

          B Offline
          B Offline
          Bob_Sun
          wrote on last edited by
          #6

          I was just told that lock(this) is a bad sample which will bring about unlocked problem but no further back up. I found the following forum instead. And I am a little bit puzzled. http://msdn.microsoft.com/msdnmag/issues/03/01/NET/[^]

          1 Reply Last reply
          0
          • B Bob_Sun

            Soory for made a mistake to post 2 messages. I think it is better to use this thread only

            G Offline
            G Offline
            Guffa
            wrote on last edited by
            #7

            Bob_Sun wrote:

            I think it is better to use this thread only

            I already replied in the first thread. --- b { font-weight: normal; }

            B 1 Reply Last reply
            0
            • S Steve Hansen

              lock(this) can bring you in a deadlock instead create a field like this: private object syncRoot = new object(); and use lock(syncRoot) [EDIT] Not static :) field will be static only for static methods

              B Offline
              B Offline
              Bob_Sun
              wrote on last edited by
              #8

              Thank you very much. lock( a private object ) was the good way introduced to avoid deadlock. But why lock(this) brings about deadlock? I want to find the case in which my program falls into a deadlock.

              S 1 Reply Last reply
              0
              • G Guffa

                Bob_Sun wrote:

                I think it is better to use this thread only

                I already replied in the first thread. --- b { font-weight: normal; }

                B Offline
                B Offline
                Bob_Sun
                wrote on last edited by
                #9

                Thank you very much. I have read your following reply.

                Who told you that it wasn't a good way, and what was the arguments for that?

                I was just told so, and I found the following forum. http://msdn.microsoft.com/msdnmag/issues/03/01/NET/[^]

                Locking should of course be kept as a minimum, and to avoid deadlocks a thread should never lock more than one object at a time. Still, for sharing data between threads there is hardly any alternative.

                I wrote a class for statemachine, whenever a event is dispatched, the whole statemachine should be locked in the thread. As for number of objects locked in this thread, it is always more than 2. As this is a statemachine, state changes must be sent to upper class. As the so called upper class collects messages from many child classes, messages are first enqueued before processed. So, in this thread lock(this) // Statemachine { ... lock(this) // MessageQueue { ... } } When several events occured in different threads, I suppose all of them will be locked at the first lock(this) // Statemachine before processed. Will this cause a deadlock in my program ?

                G 1 Reply Last reply
                0
                • B Bob_Sun

                  Thank you very much. lock( a private object ) was the good way introduced to avoid deadlock. But why lock(this) brings about deadlock? I want to find the case in which my program falls into a deadlock.

                  S Offline
                  S Offline
                  Steve Hansen
                  wrote on last edited by
                  #10

                  This article seems to mention a DeadLock http://www.codeproject.com/csharp/lockmanager.asp

                  1 Reply Last reply
                  0
                  • B Bob_Sun

                    Thank you very much. I have read your following reply.

                    Who told you that it wasn't a good way, and what was the arguments for that?

                    I was just told so, and I found the following forum. http://msdn.microsoft.com/msdnmag/issues/03/01/NET/[^]

                    Locking should of course be kept as a minimum, and to avoid deadlocks a thread should never lock more than one object at a time. Still, for sharing data between threads there is hardly any alternative.

                    I wrote a class for statemachine, whenever a event is dispatched, the whole statemachine should be locked in the thread. As for number of objects locked in this thread, it is always more than 2. As this is a statemachine, state changes must be sent to upper class. As the so called upper class collects messages from many child classes, messages are first enqueued before processed. So, in this thread lock(this) // Statemachine { ... lock(this) // MessageQueue { ... } } When several events occured in different threads, I suppose all of them will be locked at the first lock(this) // Statemachine before processed. Will this cause a deadlock in my program ?

                    G Offline
                    G Offline
                    Guffa
                    wrote on last edited by
                    #11

                    If those are the only lock statements in the code, and they always get called in that order, it won't cause a deadlock. But if they always get called in that order, the inner lock is of no use at all. --- b { font-weight: normal; }

                    B 1 Reply Last reply
                    0
                    • G Guffa

                      If those are the only lock statements in the code, and they always get called in that order, it won't cause a deadlock. But if they always get called in that order, the inner lock is of no use at all. --- b { font-weight: normal; }

                      B Offline
                      B Offline
                      Bob_Sun
                      wrote on last edited by
                      #12

                      Guffa wrote:

                      If those are the only lock statements in the code, and they always get called in that order, it won't cause a deadlock. But if they always get called in that order, the inner lock is of no use at all.

                      First, the second lock(this) is not necessarily called every time, but when called the order is always the same. Second, the second lock(this) may also be called directly from other threads which have no relation to StateMachine(outside statemachine).

                      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