Click here to Skip to main content
15,914,384 members
Home / Discussions / C#
   

C#

 
Generalc# Stream buffer problem Pin
Liskl11-Jun-04 19:14
Liskl11-Jun-04 19:14 
GeneralRe: c# Stream buffer problem Pin
leppie11-Jun-04 20:53
leppie11-Jun-04 20:53 
GeneralRe: c# Stream buffer problem Pin
Heath Stewart12-Jun-04 4:49
protectorHeath Stewart12-Jun-04 4:49 
Generaljetaudio5, winamp5 and wmp9 Pin
Taha Elsayed11-Jun-04 16:29
Taha Elsayed11-Jun-04 16:29 
GeneralRe: jetaudio5, winamp5 and wmp9 Pin
Arjan Einbu11-Jun-04 23:22
Arjan Einbu11-Jun-04 23:22 
GeneralFinding and repeating a character Pin
Rostrox11-Jun-04 15:23
Rostrox11-Jun-04 15:23 
GeneralRe: Finding and repeating a character Pin
Rostrox12-Jun-04 3:40
Rostrox12-Jun-04 3:40 
GeneralThread locking Pin
link_7911-Jun-04 12:57
link_7911-Jun-04 12:57 
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.Smile | :)


GeneralRe: Thread locking Pin
Xiangyang Liu 刘向阳11-Jun-04 13:28
Xiangyang Liu 刘向阳11-Jun-04 13:28 
GeneralRe: Thread locking Pin
link_7911-Jun-04 13:48
link_7911-Jun-04 13:48 
GeneralRe: Thread locking Pin
Meysam Mahfouzi11-Jun-04 17:21
Meysam Mahfouzi11-Jun-04 17:21 
GeneralRe: Thread locking Pin
Xiangyang Liu 刘向阳12-Jun-04 3:01
Xiangyang Liu 刘向阳12-Jun-04 3:01 
GeneralRe: Thread locking Pin
link_7913-Jun-04 9:57
link_7913-Jun-04 9:57 
GeneralRe: Thread locking Pin
Meysam Mahfouzi11-Jun-04 17:13
Meysam Mahfouzi11-Jun-04 17:13 
GeneralPlaceHolder, TreeView, UserControl Pin
caheo11-Jun-04 9:22
caheo11-Jun-04 9:22 
GeneralRe: PlaceHolder, TreeView, UserControl Pin
Heath Stewart11-Jun-04 10:40
protectorHeath Stewart11-Jun-04 10:40 
Questioninefficient to create new delegate every time? Pin
vista2711-Jun-04 8:10
vista2711-Jun-04 8:10 
AnswerRe: inefficient to create new delegate every time? Pin
Heath Stewart11-Jun-04 8:48
protectorHeath Stewart11-Jun-04 8:48 
QuestionRichTextBox cannot handle huge amount of color-coded, varied font text? Pin
vista2711-Jun-04 7:47
vista2711-Jun-04 7:47 
AnswerRe: RichTextBox cannot handle huge amount of color-coded, varied font text? Pin
Aaron Eldreth11-Jun-04 8:32
Aaron Eldreth11-Jun-04 8:32 
GeneralRe: RichTextBox cannot handle huge amount of color-coded, varied font text? Pin
vista2711-Jun-04 9:16
vista2711-Jun-04 9:16 
GeneralRe: RichTextBox cannot handle huge amount of color-coded, varied font text? Pin
Aaron Eldreth11-Jun-04 12:18
Aaron Eldreth11-Jun-04 12:18 
GeneralUpdate to DB Failure Pin
MrJJKoolJ11-Jun-04 7:44
MrJJKoolJ11-Jun-04 7:44 
GeneralRe: Update to DB Failure Pin
Colin Angus Mackay11-Jun-04 8:10
Colin Angus Mackay11-Jun-04 8:10 
GeneralRe: Update to DB Failure Pin
MrJJKoolJ11-Jun-04 8:33
MrJJKoolJ11-Jun-04 8:33 

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.