Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

.NET - Is the Queue.Enqueue method thread safe?

Let's say that I have a module that has a Queue in it.

For other entities to Enqueue, they must go through a function:

public sub InsertIntoQueue(Obj)
    MyQueue.Enqueue(Obj)
end sub

If I have multiple threads running and they want to call InsertIntoQueue(), is this considered thread safe?

I am under the impression that there is only one copy of the instructions in memory necessary to perform the InsertIntoQueue() function... which would lead me to think that this is thread safe.

However, I wonder what happens when two threads attempt to run the function at the same time?

Is this thread safe, and if not, how can I make it thread safe? (and What would be the performance implications regarding speed and memory usage)

like image 547
Brian Webster Avatar asked Nov 02 '09 18:11

Brian Webster


1 Answers

Use Queue.Synchronized wrapper.

like image 75
manji Avatar answered Oct 12 '22 23:10

manji