title: Accessing the youngest element of a ConcurrentQueue in presence of multiple threads operating on the queue
body: <p>We have a ConcurrentQueue which is used to share data among 3 threads. Thread A continuously fills the queue with data. Thread B is designed to record this data to a file. Thread C is supposed to retrieve the youngest entry in the queue (or as close to youngest as possible), perform some operations on it and display results on the screen.</p>
  
  <p>Thread B, in order to cluster the file write operations in time, does something like this:</p>
  
  <pre><code>if (cq.Count &gt; 100)
  {
      while (cq.Count &gt; 1)
      {
          qElement = PopFromCq(cq); // PopFromCq uses cq.TryDequeue()
          bw.Write(qElement.data); // bw is a binary writer
      }
  }
  else
  {
      System.Threading.Thread.Sleep(10);
  }
  </code></pre>
  
  <p>ie, it waits for at least 100 elements to queue up, then writes them to the disk. It always maintains at least one item in the queue though and the reason is because we want Thread C to always have access to at least one item.</p>
  
  <p>The loop in thread C looks like:</p>
  
  <pre><code>while (threadsRunning) 
  {
      System.Threading.Thread.Sleep(500); // Update twice per second
      ProcessDataAndUpdateScreen(cq.ElementAt(cq.Count - 1)); // our terrible attempt at looking at the latest (or close to latest) entry in the queue
  }
  </code></pre>
  
  <p>In this loop, we sometimes get an exception due to the race between the thread that writes the data to disk, and the cq.ElementAt(cq.Count-1) call. I believe what is happening is as follows:</p>
  
  <ol>
  <li>cq.Count is calculated to be, say 90.</li>
  <li>By that time, Thread B already started its loop and it is dequeueing data from the queue to write to the disk</li>
  <li>By the time cq.ElementAt() is called, Thread B consumed a number of items such that (cq.Count - 1) no longer points to a valid entry in the queue.</li>
  </ol>
  
  <p>Any ideas on what would be a nice way of accessing the youngest entry in the queue in presence of multiple threads operating on the queue?</p>
  
  <p>Regards,</p>