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

  • Home
  • SEARCH
  • 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 221273
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 11, 20262026-05-11T19:01:04+00:00 2026-05-11T19:01:04+00:00

I have a thread which produces data in the form of simple object (record).

  • 0

I have a thread which produces data in the form of simple object (record). The thread may produce a thousand records for each one that successfully passes a filter and is actually enqueued. Once the object is enqueued it is read-only.

I have one lock, which I acquire once the record has passed the filter, and I add the item to the back of the producer_queue.

On the consumer thread, I acquire the lock, confirm that the producer_queue is not empty,
set consumer_queue to equal producer_queue, create a new (empty) queue, and set it on producer_queue. Without any further locking I process consumer_queue until it’s empty and repeat.

Everything works beautifully on most machines, but on one particular dual-quad server I see in ~1/500k iterations an object that is not fully initialized when I read it out of consumer_queue. The condition is so fleeting that when I dump the object after detecting the condition the fields are correct 90% of the time.

So my question is this: how can I assure that the writes to the object are flushed to main memory when the queue is swapped?

Edit:

On the producer thread:
(producer_queue above is m_fillingQueue; consumer_queue above is m_drainingQueue)

private void FillRecordQueue() {
  while (!m_done) {
    int count;
    lock (m_swapLock) {
      count = m_fillingQueue.Count;
    }
    if (count > 5000) {
      Thread.Sleep(60);
    } else {
      DataRecord rec = GetNextRecord();
      if (rec == null) break;
      lock (m_swapLock) {
        m_fillingQueue.AddLast(rec);
      }
    }
  }
}

In the consumer thread:

private DataRecord Next(bool remove) {
  bool drained = false;
  while (!drained) {
    if (m_drainingQueue.Count > 0) {
      DataRecord rec = m_drainingQueue.First.Value;
      if (remove) m_drainingQueue.RemoveFirst();
      if (rec.Time < FIRST_VALID_TIME) {
        throw new InvalidOperationException("Detected invalid timestamp in Next(): " + rec.Time + " from record " + rec);
      }
      return rec;
    } else {
      lock (m_swapLock) {
        m_drainingQueue = m_fillingQueue;
        m_fillingQueue = new LinkedList<DataRecord>();
        if (m_drainingQueue.Count == 0) drained = true;
      }
    }
  }
  return null;
}

The consumer is rate-limited, so it can’t get ahead of the consumer.

The behavior I see is that sometimes the Time field is reading as DateTime.MinValue; by the time I construct the string to throw the exception, however, it’s perfectly fine.

  • 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-11T19:01:05+00:00Added an answer on May 11, 2026 at 7:01 pm

    Assuming these are in fact the only methods that interact with the m_fillingQueue variable, and that DataRecord cannot be changed after GetNextRecord() creates it (read-only properties hopefully?), then the code at least on the face of it appears to be correct.

    In which case I suggest that GregC’s answer would be the first thing to check; make sure the failing machine is fully updated (OS / drivers / .NET Framework), becasue the lock statement should involve all the required memory barriers to ensure that the rec variable is fully flushed out of any caches before the object is added to the list.

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

Sidebar

Related Questions

I have a thread which produces some data (a python list) and which shall
I have one std::list<> container and these threads: One writer thread which adds elements
I have a layered application in Java which has a multi thread data access
I have main thread which calls another thread. timeout period of second one is
I have thread exception handler which saves the exception stack trace and should close
I'm working on a thread library which implement user level threads (i have something
In my C# program, I have a thread that represents a running test, which
I have a class which is not thread safe: class Foo { /* Abstract
I have one thread that writes results into a Queue. In another thread (GUI),
I have an archive object which manages various byte arrays and hands out InputStream

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.