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 6115657
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 23, 20262026-05-23T15:07:00+00:00 2026-05-23T15:07:00+00:00

Consider the following code, which takes place in a background thread (thread B): List<T>

  • 0

Consider the following code, which takes place in a background thread (“thread B”):

List<T> invocationQueueCopy;
lock (invocationQueue)
{
    invocationQueueCopy = invocationQueue;
    invocationQueue = new List<T>();
}

In another thread (“thread A”) I simply lock “invocationQueue” before adding to it:

lock (invocationQueue)
{
    invocationQueue.Add(args);
}

I have read that reference assignment is atomic, but could it ever occur that “thread A” will end up writing to the old list (the one replaced in “thread B”) after receiving the lock? I have read other answers that imply it could, if the value of the reference were stored in a register on “thread A” then it would not know that “thread B” had modified the value in the class. If this is so, would declaring “invocationQueue” volatile prevent this?

Notes:

  • I know I could clone then clear the list.
  • I
    know I could have a separate lock
    object for the list.

But I’d rather not do either of these things unless it is required.

Thanks in advance.

Edit:

Just to clarify from Adam’s comments: invocationQueue is a private field which is created internally to this class and never exposed to the outside world so nothing could lock on it except these two methods.

  • 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-23T15:07:01+00:00Added an answer on May 23, 2026 at 3:07 pm

    EDIT: Your solution will work. Lock creates a full fence so any caching is prevented, basically meaning you’ll always get the most recent value for the list reference. The only thing, as suggested in the comments is the fact that you should do the locking on a neutral object, not the list itself.

    The following is wrong!! But I let it here anyway to show how fu*** hard threading might be… the fact is the following reasoning is defeated by the fact that lock creates a full fence.

    Yeah, it can happen so don’t do it
    that way.

    It won’t get better even if you did
    the lock into a readonly whatever
    object.

    See what might happen (although most
    of the time it WON’T happen).

    ThreadA and ThreadB are executing on
    different processors, each one with
    its own cache memory which holds the
    reference to incovationQueue.

    • ThreadB locks invocationQueue, the lock is done to a reference which is
      taken for the cache of processor1, not
      to a variable name.
    • ThreadB copies the invocationQueue.
    • ThreadA locks invocationQueue, the lock is done to a reference which is
      taken for the cache on processor2 and
      which, in this moment is the same as
      the one in processor1, and starts
      waiting.
    • ThreadB creates a new List and assigns it to the invocationQueue, the
      cache in the processor1 is updated but
      since the variable is NOT volatile
      that’s all that happens.
    • ThreadA enters the lock and gets the reference from his cache, which points
      to the old reference, therefore you
      end up adding the variable to the old
      list.

    So you need to make the list volatile
    AND use the lock if you’re going to be
    playing with the reference itself.

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

Sidebar

Related Questions

Consider the following code: Dim arr1 As New List(Of Double) Dim arr2 As New
Consider the following code, which simply calls a method on each member of a
Consider the following code which is typcial of many ChannelFactory examples: WSHttpBinding myBinding =
Consider the following code which shows compile time error : #include <stdio.h> int main(int
consider the following code, which represents an attempt to implement partial matching. the intended
Let's consider the following scenario: a function which can generate code colors from white
Consider the following code which is to be thrown at an AR find: conditions
I've got some basic questions about C++. Consider the following code in which I
Consider the following C# code: Decimal number = new decimal(8.0549); Decimal rounded = Math.Round(number,
Consider the following code which uses non-blocking semantics to pop a stack: T Stack<T>::pop(

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.