Click here to Skip to main content
16,005,389 members
Home / Discussions / C#
   

C#

 
GeneralRe: trim byte[] to new size - after socket.receive Pin
Ista10-Aug-03 13:14
Ista10-Aug-03 13:14 
Generalcontrol the connection Pin
Ahmed Mazrou39-Aug-03 17:16
Ahmed Mazrou39-Aug-03 17:16 
GeneralRe: control the connection Pin
Ista10-Aug-03 10:18
Ista10-Aug-03 10:18 
GeneralIF STATMENT Pin
eggie59-Aug-03 17:01
eggie59-Aug-03 17:01 
GeneralRe: IF STATMENT Pin
eggie59-Aug-03 17:41
eggie59-Aug-03 17:41 
GeneralRe: IF STATMENT Pin
Csharp™10-Aug-03 0:25
Csharp™10-Aug-03 0:25 
GeneralRe: IF STATMENT Pin
eggie510-Aug-03 18:23
eggie510-Aug-03 18:23 
GeneralPlease help decipher this MSDN threading sample! Pin
devvvy9-Aug-03 16:26
devvvy9-Aug-03 16:26 
Thanks for taking time to respond to my question. I'm going over a threading sample from MSDN and is having a hard time trying to understand the sample:
(a) Monitor.Wait
(b) Monitor.Pulse
(c) waiting Vs ready queue in context of threading
(d) Why you need to add a "readerFlag" in the resource you are acquiring a lock on when you have already locked the resource using "lock( )" statement.

Before going further, reference to MSDN here:
http://msdn.microsoft.com/library/default.asp?url=/library/en-us/csref/html/vcwlkthreadingtutorial.asp

QUESTION 1:
"A lot of times there are a couple of steps to locking an object"
>> Q1-a: I don't get it. To lock and unlock an object:

Monitor.Enter(thisObj); //This won't return until you get the lock
thisObj.DoSomething();
Monitor.Exit(thisObj);

To try to lock and obj, use Monitor.TryEnter instead. But, in our MSDN sample:

public void WriteToCell(int n)
{
lock(this) // Enter synchronization block: The "lock()" block.
{
if (readerFlag)
{ // Why do you need this flag if you have already "lock" the resource???
}

} // Q1-b: Lock release here - my understanding correct?? This means you can do whatever you want in the "lock" block.? (Exiting THE "lock()"-block)

}
}

>> Q1-c: Once you acquire the lock (via Monitor.Enter or lock() function as in sample), NO other thread can acquire lock on "thisObj" until "Monitor.Exit". There's NO need for "readerFlag".


QUESTION 2:
Quote:
"Now that the cook is done, you go "hey over there, it's my turn! No one else is allowed to cut in front of me!" so you call
[
Monitor.Wait(this);
]"

>> Q2-a: Doesn't Wait function surrender lock to resource to next thread in "waiting queue". According to MSDN, Wait() does this:
* "Releases the lock on an object in order to permit other threads (in waiting queue) to lock and access the object."
* "Releases the lock on an object and blocks the current thread until it reacquires the lock."
My question is, why release lock BEFORE you actually do something (Read) with reseource?

public int ReadFromCell( )
{
lock(this) // Enter synchronization block: The "lock()"-block
{
if (!readerFlag) //WhY?? You ALREADY have the lock on "this" resource.
{
try
{
// Again, WHY!? You ALREADY have lock on "this" resource.
Monitor.Wait(this);
}
... catch statements...
}
Console.WriteLine("Consume: {0}",cellContents); //Q2-b: NOW you're reading - but why can't you read immediately after "lock(this)" statement???

readerFlag = false; // Q2-c: I have no idea why we need this flag.
// is done.
Monitor.Pulse(this); // Q2-d: I have no idea why we need to pulse - exitining the "lock()" block surrender the lock, other threads waiting for the resources will naturally be able to acquire it once execution exit the "lock()" block, why Pulse???

} // Exit synchronization block: The "lock()"-block
return cellContents;
}

>> Q2-f: waiting queue Vs ready queue - What the... It's in Monitor class documentation (.NET framework).

Help!
GeneralRe: Please help decipher this MSDN threading sample! Pin
Meysam Mahfouzi10-Aug-03 17:31
Meysam Mahfouzi10-Aug-03 17:31 
GeneralRe: Please help decipher this MSDN threading sample! Pin
devvvy11-Aug-03 2:56
devvvy11-Aug-03 2:56 
GeneralRe: Please help decipher this MSDN threading sample! Pin
devvvy13-Aug-03 6:57
devvvy13-Aug-03 6:57 
GeneralWSDL question Pin
Mazdak9-Aug-03 9:52
Mazdak9-Aug-03 9:52 
GeneralRe: WSDL question Pin
James T. Johnson9-Aug-03 11:27
James T. Johnson9-Aug-03 11:27 
GeneralRe: WSDL question Pin
Ista9-Aug-03 12:13
Ista9-Aug-03 12:13 
GeneralRe: WSDL question Pin
Mazdak10-Aug-03 1:54
Mazdak10-Aug-03 1:54 
GeneralRe: WSDL question Pin
Ista10-Aug-03 9:53
Ista10-Aug-03 9:53 
Generalcode for email address validation on logon web application Pin
gilstelzer9-Aug-03 6:09
gilstelzer9-Aug-03 6:09 
GeneralRe: code for email address validation on logon web application Pin
leppie9-Aug-03 7:36
leppie9-Aug-03 7:36 
GeneralNUnit testing in Visual Studio Pin
Will Ballard9-Aug-03 4:27
Will Ballard9-Aug-03 4:27 
GeneralFinding Char Index in current line Pin
.gonad9-Aug-03 2:35
.gonad9-Aug-03 2:35 
GeneralRe: Finding Char Index in current line Pin
Ista9-Aug-03 5:12
Ista9-Aug-03 5:12 
GeneralRe: simple messagebox Pin
J. Dunlap8-Aug-03 22:42
J. Dunlap8-Aug-03 22:42 
GeneralRe: simple messagebox Pin
Ista9-Aug-03 9:53
Ista9-Aug-03 9:53 
GeneralBarcode reader and Receipt printer Pin
ChiYung8-Aug-03 19:53
ChiYung8-Aug-03 19:53 
GeneralRe: Barcode reader and Receipt printer Pin
AdrianT9-Aug-03 1:12
AdrianT9-Aug-03 1:12 

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.