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