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. Thread locking

Thread locking

Scheduled Pinned Locked Moved C#
questionlearning
7 Posts 3 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.
  • L Offline
    L Offline
    link_79
    wrote on last edited by
    #1

    Hi all, I need some major clarification on the thread locking mechanism. This might be a bit long so please bear with me. Supposing we have a class definition as follows: Class X { private Y obj1; private Z obj2; public void Ftn1() { lock (this) // section 1 { obj1.ModifyThis(); } } public void Ftn2() { lock (obj2) // section 2 { obj2.ModifyMe(); } } public void Ftn3() { lock (this) // section 3 { obj2.ModifyMe(); } } } // main ftn. X mainObj; Thread A new ThreadStart(mainObj.Ftn1()); Thread B new ThreadStart(mainObj.Ftn2()); Thread C new ThreadStart(mainObj.Ftn3()); // start them all now My question are: Q1. Since Ftn1 has a 'lock(this)' code block, if thread 'A' started first, will 'B' and 'C' have to wait till 'A' is done (even though their jobs are to do ftn2 and ftn3. I ask this because, 'X' has been explicitly locked in the call to 'Ftn1', and 'Ftn2' and 'Ftn3' belong to this 'X'? Note: My current understanding is that the 'lock(this)' call is actually only locking that section of code i.e. only this method is locked, but the 'this' now has me confused and I need it cleared up before I proceed any further. Q2. If you look at Ftn2 and Ftn3, they lock different things, but acces the same resource, so my question is if Ftn2 fires first, then will all go well i.e. Ftn3 will have to wait till the lock on obj2 is released, but if ftn3 fires first, there is a possibility that obj2 will get a corrupted result but deadlock will never occur? If my assumptions are true then I can continue on with endless coding more satisfied and releived. Any other input or advice will also be very welcome. Thanks for reading.:)

    X M 2 Replies Last reply
    0
    • L link_79

      Hi all, I need some major clarification on the thread locking mechanism. This might be a bit long so please bear with me. Supposing we have a class definition as follows: Class X { private Y obj1; private Z obj2; public void Ftn1() { lock (this) // section 1 { obj1.ModifyThis(); } } public void Ftn2() { lock (obj2) // section 2 { obj2.ModifyMe(); } } public void Ftn3() { lock (this) // section 3 { obj2.ModifyMe(); } } } // main ftn. X mainObj; Thread A new ThreadStart(mainObj.Ftn1()); Thread B new ThreadStart(mainObj.Ftn2()); Thread C new ThreadStart(mainObj.Ftn3()); // start them all now My question are: Q1. Since Ftn1 has a 'lock(this)' code block, if thread 'A' started first, will 'B' and 'C' have to wait till 'A' is done (even though their jobs are to do ftn2 and ftn3. I ask this because, 'X' has been explicitly locked in the call to 'Ftn1', and 'Ftn2' and 'Ftn3' belong to this 'X'? Note: My current understanding is that the 'lock(this)' call is actually only locking that section of code i.e. only this method is locked, but the 'this' now has me confused and I need it cleared up before I proceed any further. Q2. If you look at Ftn2 and Ftn3, they lock different things, but acces the same resource, so my question is if Ftn2 fires first, then will all go well i.e. Ftn3 will have to wait till the lock on obj2 is released, but if ftn3 fires first, there is a possibility that obj2 will get a corrupted result but deadlock will never occur? If my assumptions are true then I can continue on with endless coding more satisfied and releived. Any other input or advice will also be very welcome. Thanks for reading.:)

      X Offline
      X Offline
      Xiangyang Liu
      wrote on last edited by
      #2

      Answer to Q1: Fcn1 and Fcn3 will be synchronized, because they both lock "this", only one of them can go on at a time. Fcn2 will execute regardless what happens in Fcn1 and Fcn3, because it is locking a different object (obj2). Answer to Q2: Fcn2 and Fcn3 can execute concurrently, since they are modifying the same object, the code will not be thread-safe. In general, if two blocks of code are locking the same object, then only one of them can be executed, the other will wait its turn. If they are locking different objects, even if one of the objects being locked is a member of the other object, the two blocks of code can execute concurrently. Hope this helps.[

      My articles and software tools

      ](http://mysite.verizon.net/XiangYangL/index.htm)

      L 1 Reply Last reply
      0
      • X Xiangyang Liu

        Answer to Q1: Fcn1 and Fcn3 will be synchronized, because they both lock "this", only one of them can go on at a time. Fcn2 will execute regardless what happens in Fcn1 and Fcn3, because it is locking a different object (obj2). Answer to Q2: Fcn2 and Fcn3 can execute concurrently, since they are modifying the same object, the code will not be thread-safe. In general, if two blocks of code are locking the same object, then only one of them can be executed, the other will wait its turn. If they are locking different objects, even if one of the objects being locked is a member of the other object, the two blocks of code can execute concurrently. Hope this helps.[

        My articles and software tools

        ](http://mysite.verizon.net/XiangYangL/index.htm)

        L Offline
        L Offline
        link_79
        wrote on last edited by
        #3

        This definitely helps, thank so much. But then how can I ever do the following: If say a class 'Service' offers three services: Class Service { void Service1() { // modify x } void Service 2() { // modify y } void Service3() { // modify z } } and I there are 'n' number of threads, each wanting to do only 1 of these services. I want the ability to be able to only do 'method' locks, not lock the whole object meaning that services should be able to run simultaneously, becuase I know that these services modify different resources. No 2 thread should be able to enter the same service. C# only provides for lock(...) { } there is no syntax like lock // this block of code { } or is there? Thanks again.

        M X 2 Replies Last reply
        0
        • L link_79

          Hi all, I need some major clarification on the thread locking mechanism. This might be a bit long so please bear with me. Supposing we have a class definition as follows: Class X { private Y obj1; private Z obj2; public void Ftn1() { lock (this) // section 1 { obj1.ModifyThis(); } } public void Ftn2() { lock (obj2) // section 2 { obj2.ModifyMe(); } } public void Ftn3() { lock (this) // section 3 { obj2.ModifyMe(); } } } // main ftn. X mainObj; Thread A new ThreadStart(mainObj.Ftn1()); Thread B new ThreadStart(mainObj.Ftn2()); Thread C new ThreadStart(mainObj.Ftn3()); // start them all now My question are: Q1. Since Ftn1 has a 'lock(this)' code block, if thread 'A' started first, will 'B' and 'C' have to wait till 'A' is done (even though their jobs are to do ftn2 and ftn3. I ask this because, 'X' has been explicitly locked in the call to 'Ftn1', and 'Ftn2' and 'Ftn3' belong to this 'X'? Note: My current understanding is that the 'lock(this)' call is actually only locking that section of code i.e. only this method is locked, but the 'this' now has me confused and I need it cleared up before I proceed any further. Q2. If you look at Ftn2 and Ftn3, they lock different things, but acces the same resource, so my question is if Ftn2 fires first, then will all go well i.e. Ftn3 will have to wait till the lock on obj2 is released, but if ftn3 fires first, there is a possibility that obj2 will get a corrupted result but deadlock will never occur? If my assumptions are true then I can continue on with endless coding more satisfied and releived. Any other input or advice will also be very welcome. Thanks for reading.:)

          M Offline
          M Offline
          Meysam Mahfouzi
          wrote on last edited by
          #4

          link_79 wrote: Since Ftn1 has a 'lock(this)' code block, if thread 'A' started first, will 'B' and 'C' have to wait till 'A' is done Yes they have, because all the instance have been Locked.

          1 Reply Last reply
          0
          • L link_79

            This definitely helps, thank so much. But then how can I ever do the following: If say a class 'Service' offers three services: Class Service { void Service1() { // modify x } void Service 2() { // modify y } void Service3() { // modify z } } and I there are 'n' number of threads, each wanting to do only 1 of these services. I want the ability to be able to only do 'method' locks, not lock the whole object meaning that services should be able to run simultaneously, becuase I know that these services modify different resources. No 2 thread should be able to enter the same service. C# only provides for lock(...) { } there is no syntax like lock // this block of code { } or is there? Thanks again.

            M Offline
            M Offline
            Meysam Mahfouzi
            wrote on last edited by
            #5

            link_79 wrote: No 2 thread should be able to enter the same service link_79 wrote: I know that these services modify different resources In each method, just lock the variables modified in that method, this way, each method is somehow locked separately and services will run simultaneously.


            I lock my house, I lock my car and I pull that zipper on my pants up several times a day, just for the sake of security.

            1 Reply Last reply
            0
            • L link_79

              This definitely helps, thank so much. But then how can I ever do the following: If say a class 'Service' offers three services: Class Service { void Service1() { // modify x } void Service 2() { // modify y } void Service3() { // modify z } } and I there are 'n' number of threads, each wanting to do only 1 of these services. I want the ability to be able to only do 'method' locks, not lock the whole object meaning that services should be able to run simultaneously, becuase I know that these services modify different resources. No 2 thread should be able to enter the same service. C# only provides for lock(...) { } there is no syntax like lock // this block of code { } or is there? Thanks again.

              X Offline
              X Offline
              Xiangyang Liu
              wrote on last edited by
              #6

              link_79 wrote: Class Service { void Service1() { // modify x } void Service 2() { // modify y } void Service3() { // modify z } } A simple solution is:

              Class Service
              {
              void Service1()
              {
              lock(x)
              {
              // modify x
              }
              }
              void Service 2()
              {
              lock(y)
              {
              // modify y
              }
              }
              void Service3()
              {
              lock(z)
              {
              // modify z
              }
              }
              }

              When I say "lock" a block of code, I meant lock(TheObjectToBeLocked) { // block of code } :)[

              My articles and software tools

              ](http://mysite.verizon.net/XiangYangL/index.htm)

              L 1 Reply Last reply
              0
              • X Xiangyang Liu

                link_79 wrote: Class Service { void Service1() { // modify x } void Service 2() { // modify y } void Service3() { // modify z } } A simple solution is:

                Class Service
                {
                void Service1()
                {
                lock(x)
                {
                // modify x
                }
                }
                void Service 2()
                {
                lock(y)
                {
                // modify y
                }
                }
                void Service3()
                {
                lock(z)
                {
                // modify z
                }
                }
                }

                When I say "lock" a block of code, I meant lock(TheObjectToBeLocked) { // block of code } :)[

                My articles and software tools

                ](http://mysite.verizon.net/XiangYangL/index.htm)

                L Offline
                L Offline
                link_79
                wrote on last edited by
                #7

                Geez, that solution was staring me in the face, but my internal bulb never went off.:doh: My doubts with the 'lock(this)', threw me off course.:laugh: Thanks for all the help guys.

                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