System.Threading.Monitor question
From: Jon Shemitz (jon_at_midnightbeach.com)
Date: 02/16/04
- Next message: James Crowley: "Re: Read/write simple XML"
- Previous message: Daryl Davis: "MTS vs .net Framework"
- Next in thread: Alvin Bruney [MVP]: "Re: System.Threading.Monitor question"
- Reply: Alvin Bruney [MVP]: "Re: System.Threading.Monitor question"
- Messages sorted by: [ date ] [ thread ]
Date: Mon, 16 Feb 2004 12:55:48 -0800
Exploring the C# lock, I wrote the simple multi-threaded IEnumerator
consumer, below. It simply reads elements and adds them to List, a
private ArrayList member of the same object as threadProc().
I originally wrote this without the Thread.Sleep(0); line, and I found
that - no matter how big the list or how many threads - one thread
would consume the whole list. Adding the Thread.Sleep(0); line let the
threads share the list as expected.
>From this I conclude
> lock() (System.Threading.Monitor.Enter()) will not block
> if another thread is waiting on the same object;
> only if another thread already has it locked.
Correct?
private void threadProc()
{
object Current;
do
{
lock (Enumerator)
Current = Enumerator.MoveNext()
? Enumerator.Current
: null;
if (Current != null)
List.Add(Current);
Thread.Sleep(0);
} while (Current != null);
}
-- programmer, author http://www.midnightbeach.com and father http://www.midnightbeach.com/hs
- Next message: James Crowley: "Re: Read/write simple XML"
- Previous message: Daryl Davis: "MTS vs .net Framework"
- Next in thread: Alvin Bruney [MVP]: "Re: System.Threading.Monitor question"
- Reply: Alvin Bruney [MVP]: "Re: System.Threading.Monitor question"
- Messages sorted by: [ date ] [ thread ]
Relevant Pages
|