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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 10, 20262026-06-10T07:38:24+00:00 2026-06-10T07:38:24+00:00

Would it be thread-safe if I solely call Enqueue(T) from multiple threads simultaneously and

  • 0

Would it be thread-safe if I solely call Enqueue(T) from multiple threads simultaneously and wait for those threads to complete before calling Dequeue() or enumerating the queue?

var queue = new Queue<int>();
Action enqueue = () =>
{
    for (int i = 0; i < 100000; i++)
        queue.Enqueue(i);
};

var tasks = new[]
{
    new Task(enqueue),
    new Task(enqueue),
    new Task(enqueue)
};
foreach (var task in tasks)
    task.Start();

Task.Factory.ContinueWhenAll(tasks, t =>
{
    while (queue.Count > 0)
        Console.WriteLine(queue.Dequeue());
});
  • 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-06-10T07:38:25+00:00Added an answer on June 10, 2026 at 7:38 am

    The documentation also states that instance members of this type are not thread safe (scroll down to the Thread Safety section).

    The documentation also states:

    A Queue can support multiple readers concurrently, as long as the
    collection is not modified.

    However, this is just a by-product of the fact that concurrent reading does not mutate the list. It does not make the type “thread-safe”. Thread-safety is best thought of as offering true support across all actions that define the public contract of the type (in this case, thread-safety in mutating the list also).

    More tongue-in-cheek: the implementation of Enqueue doesn’t include any thread synchronisation or locking primitives:

    public void Enqueue(T item)
    {
        if (this._size == this._array.Length)
        {
            int num = (int)((long)this._array.Length * 200L / 100L);
            if (num < this._array.Length + 4)
            {
                num = this._array.Length + 4;
            }
            this.SetCapacity(num);
        }
        this._array[this._tail] = item;
        this._tail = (this._tail + 1) % this._array.Length;
        this._size++;
        this._version++;
    }
    

    So I’m going with “no”. There is ConcurrentQueue for multi-threaded support.

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

Sidebar

Related Questions

What would be the least-slow thread-safe mechanism for controlling multiple accesses to a collection
What would be a good way to writein C# a .NET3.5 Thread Safe DataSource
I would like to separate my sigle-thread application to number of working threads. Just
How would I call a method with the below header on a thread? public
Would be thread-safe to use the yield operator inside an extension method? For example:
Does anyone know if this code would be thread safe, or do I have
Im just confused why adding to a list would not be thread safe like
I would like to implement a thread safe queue in VS 6.0 Is there
I have a thread safe observable collection replacement which I would like to write
So I want to access a singleton class from multiple threads. Conceptually I'd think

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.