Click here to Skip to main content
16,004,901 members
Home / Discussions / C#
   

C#

 
Generalusing this as synchronization object Pin
George_George22-Mar-08 3:52
George_George22-Mar-08 3:52 
GeneralRe: using this as synchronization object [modified] Pin
girm22-Mar-08 4:01
girm22-Mar-08 4:01 
GeneralRe: using this as synchronization object Pin
George_George22-Mar-08 4:03
George_George22-Mar-08 4:03 
GeneralRe: using this as synchronization object Pin
Colin Angus Mackay22-Mar-08 4:09
Colin Angus Mackay22-Mar-08 4:09 
GeneralRe: using this as synchronization object Pin
George_George23-Mar-08 2:53
George_George23-Mar-08 2:53 
GeneralRe: using this as synchronization object Pin
girm22-Mar-08 4:09
girm22-Mar-08 4:09 
GeneralRe: using this as synchronization object Pin
George_George23-Mar-08 2:54
George_George23-Mar-08 2:54 
GeneralRe: using this as synchronization object Pin
girm24-Mar-08 8:19
girm24-Mar-08 8:19 
Sure...

public class PublicClass1
{
   public PublicClass1(){};
   public void LockMe1(PublicClass2 aPublicInstance2)
   {
     lock(this) // ref line (X)
     {
         lock(aPublicInstance2)
         {
            MessageBox.ShowDialog("no deadlock, great!!");
         }
     }
}

public class PublicClass2
{
   public PublicClass2(){};
}


...
say a thread Y calls :
// aPublicInstance2 is an object of PublicClass2
// aPublicInstance1 is an object of PublicClass1
lock(aPublicInstance2) // ref line (Y)
{
   lock(aPublicInstance1)
   {
       MessageBox.ShowDialog("no deadlock, great!!");
   }
}


another thread X calls :
aPublicInstance1.LockMe1(aPublicInstance2); // aPublicInstancex is the same object as in thread Y

Lets say that thread X is actually executing '// ref line (X)'
and thread Y is actually executing '// ref line (Y)'
...no problem...

then X step forward and is now stuck because it cannot acquire the lock :
lock(aPublicInstance2) , since it is already locked by thread Y

But Y is still running and perform a call :
...and then is stuck because it cannot acquire lock on aPublicInstance1 (see 'lock(aPublicInstance1)
') because it is locked by thread X (see lock(this) // ref line (X))

...so X is blocking Y, Y is blocking X : deadlock , no message is displayed

-------------------------

Now if locked object were not publicly availlable X and Y thread would not have been able to
put lock on those objects since they wouldn't have access to it.
...
of course simply not using 'lock(this)' will not garantee you will never have deadlocks : it just limit the risks

regards
Girm
GeneralRe: using this as synchronization object Pin
George_George24-Mar-08 19:01
George_George24-Mar-08 19:01 
GeneralRe: using this as synchronization object Pin
Colin Angus Mackay22-Mar-08 4:06
Colin Angus Mackay22-Mar-08 4:06 
GeneralRe: using this as synchronization object Pin
George_George23-Mar-08 2:51
George_George23-Mar-08 2:51 
GeneralRe: using this as synchronization object Pin
Colin Angus Mackay23-Mar-08 4:06
Colin Angus Mackay23-Mar-08 4:06 
GeneralRe: using this as synchronization object Pin
George_George23-Mar-08 4:17
George_George23-Mar-08 4:17 
QuestionHow do I create a subclass dynamically from a base class? [modified] Pin
loderxp22-Mar-08 2:57
loderxp22-Mar-08 2:57 
AnswerRe: How do I create a subclass dynamically from a base class? Pin
pmarfleet22-Mar-08 3:41
pmarfleet22-Mar-08 3:41 
Questionhow to search particular word Pin
sugunavathysubramanian22-Mar-08 1:04
sugunavathysubramanian22-Mar-08 1:04 
AnswerRe: how to search particular word Pin
Christian Graus22-Mar-08 11:43
protectorChristian Graus22-Mar-08 11:43 
GeneralRe: how to search particular word Pin
sugunavathysubramanian24-Mar-08 20:24
sugunavathysubramanian24-Mar-08 20:24 
GeneralRe: how to search particular word Pin
Christian Graus24-Mar-08 21:09
protectorChristian Graus24-Mar-08 21:09 
Generalsynchronization object choosing Pin
George_George22-Mar-08 0:36
George_George22-Mar-08 0:36 
GeneralRe: synchronization object choosing Pin
Colin Angus Mackay22-Mar-08 3:12
Colin Angus Mackay22-Mar-08 3:12 
GeneralRe: synchronization object choosing Pin
George_George22-Mar-08 3:28
George_George22-Mar-08 3:28 
GeneralRe: synchronization object choosing Pin
Colin Angus Mackay22-Mar-08 3:36
Colin Angus Mackay22-Mar-08 3:36 
GeneralRe: synchronization object choosing Pin
George_George22-Mar-08 3:39
George_George22-Mar-08 3:39 
Questionhow to use UltimateSearchInput in .net with c# Pin
sugunavathysubramanian21-Mar-08 23:53
sugunavathysubramanian21-Mar-08 23:53 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.