Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 5929555
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 22, 20262026-05-22T14:26:13+00:00 2026-05-22T14:26:13+00:00

We have a ConcurrentQueue which is used to share data among 3 threads. Thread

  • 0

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.

Thread B, in order to cluster the file write operations in time, does something like this:

if (cq.Count > 100)
{
    while (cq.Count > 1)
    {
        qElement = PopFromCq(cq); // PopFromCq uses cq.TryDequeue()
        bw.Write(qElement.data); // bw is a binary writer
    }
}
else
{
    System.Threading.Thread.Sleep(10);
}

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.

The loop in thread C looks like:

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
}

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:

  1. cq.Count is calculated to be, say 90.
  2. By that time, Thread B already started its loop and it is dequeueing data from the queue to write to the disk
  3. 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.

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?

Regards,

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-22T14:26:14+00:00Added an answer on May 22, 2026 at 2:26 pm

    Is it necessary for the A-B communication and A-C communication to both go through the queue? What if you have thread A write each entry to the queue (for B to read and log) and also save the entry it’s just queued in a volatile property somewhere. Every time C wants to get the youngest element, it can just read directly from that property.

    EDIT: Instead of just relying on a volatile property, you should actually use Interlocked.CompareExchange<T>(T, T) to set and read the “youngest entry” property.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a main thread which should process data coming from several worker threads.
I have multiple threads each one with its own private concurrent queue and all
So I have something along the lines of this private List<ConcurrentQueue<int>> listOfQueues = new
I currently have a concurrent queue implementation that uses a BlockingQueue as the data
I'm using ConcurrentQueue<T> for a shared data structure which purpose is holding the last
In my current C#/NET 3.5 application, I have a task queue (thread safe) and
i have built a Producer Consumer queue wrapping a ConcurrentQueue of .net 4.0 with
Have just started using Visual Studio Professional's built-in unit testing features, which as I
Have you used VS.NET Architect Edition's Application and System diagrams to start designing a
Have you managed to get Aptana Studio debugging to work? I tried following this,

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.