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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 29, 20262026-05-29T22:40:20+00:00 2026-05-29T22:40:20+00:00

I have generic Queue<T> ( System.Collections.Generic ) which is accessed for writing from one

  • 0

I have generic Queue<T> (System.Collections.Generic) which is accessed for writing from one thread. And it must be accessed from another thread for reading.

I don’t want to do any process synchronization (which includes using ConcurrentQueue<T>) for performance reasons. So I came up with idea to copy the entire queue to another queue object of same type in the reading thread. Subsequent operations in the reading thread will be done on the copy. Copying will be done with simple operator =.

Here is some pseudo-code:

//Creating main queue
Queue<MyType> queue1 = new Queue<MyType>();

Writing thread:

//Perform writing in the main queue
queue1.Enqueue(object);
...
queue1.Dequeue();

Reading thread:

//Copy main queue 
Queue<MyType> queue2 = queue1;
//perform all operations in reading thread on queue2

So is such solution thread safe?

UPD: Thank you very much, I wasn’t aware that this is merely copying of the link. So Is there a way to copy entire object by value in thread-safe manner?

  • 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-29T22:40:20+00:00Added an answer on May 29, 2026 at 10:40 pm

    Queue<T> is a reference type. So assigning queue1 to queue2 does only copy the reference, not the queue itself.

    The assignment itself is atomic and thus thread-safe. Accessing queue1 in one thread, and queue2 in another is no safer that accessing queue1 from both of them. i.e. it is unsafe.

    I believe ConcurrentQueue<T> uses “lockless” programming techniques (Interlocked.Exchange and friends) and is pretty fast. You should benchmark it first, before excluding it as a solution.

    Copying a Queue<T> will certainly be slower than just using ConcurrentQueue<T>.


    On my 2.6GHz system ConcurrentQueue<object> manages 15 million enqueue/dequeue pairs per second compared to 40 million with with Queue<object>. So Queue<object> is about three times as fast.

    200 CPU cycles for an enqueue/dequeue pair is pretty cheap. If that is the bottleneck, try using more granular items in the queue.

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

Sidebar

Related Questions

I have a stream of data which is contained in a System::Collections::Queue . My
I have generic list which must be a preserved order so I can retrieve
I have generic async task class which fetches response from server . And i
I have created a custom generic queue which implements a generic IQueue interface, which
I am using a System.Collections.Generic , which contains instances of a class I wrote.
I have developed a generic producer-consumer queue which pulses by Monitor in the following
I have a method that receives messages from a queue of msmq. I have
I have a queue in which I can enqueue different threads, so I can
For example: Queue<System.Drawing.SolidBrush> brushQ = new Queue<System.Drawing.SolidBrush>(); ... brushQ.Clear(); If I don't explicitly dequeue
In a system I'm currently working on, I have many components which are defined

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.