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?