System.Threading.Monitor question

Tech-Archive recommends: Speed Up your PC by fixing your registry

From: Jon Shemitz (jon_at_midnightbeach.com)
Date: 02/16/04


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


Relevant Pages

  • Re: System.Threading.Monitor question
    ... necessarily means that another thread already has a lock. ... how can a thread be blocked waiting if there is no lock? ... > consumer, below. ... > private ArrayList member of the same object as threadProc. ...
    (microsoft.public.dotnet.framework)
  • Re: A design question (around threading)
    ... my application needs to be able to scan a document and upload it ... it sounds as though you need a "producer" thread that handles ... and a "consumer" thread that handles uploading. ... the lock and then enter a work loop. ...
    (microsoft.public.dotnet.languages.csharp)
  • Re: A design question (around threading)
    ... Now, I want to take advantage of the sheet feeder, as there is an initial tranch of about 50,000 documents to scan and upload, so if I get the system set up optimally, I will get a scan every 4 seconds. ... Based on the information you've provided so far, it sounds as though you need a "producer" thread that handles scanning, and a "consumer" thread that handles uploading. ... The consumer thread will acquire the lock and then enter a work loop. ...
    (microsoft.public.dotnet.languages.csharp)
  • Re: Question about vZOOM reference handling
    ... consumer message queues which go to two different consumer threads. ... But vZOOM still can be used here for readers. ... to just use a lock on the writer side. ...
    (comp.programming.threads)
  • Re: Buggy IPI and MTRR code on low memory
    ... We could have a separate lock for each destination ... Then the caller could release the lock. ... because it a) makes the caller hang around until the consumer has ... executed and it b) requires that callers still have local interrupts ...
    (Linux-Kernel)