In the following code I am using two threads to share sane resource in this example it's a queue
so do I need to use lock
while en-queueing or dequeuing
if yes then why because program seems to work fine.
class Program
{
static Queue<string> sQ = new Queue<string>();
static void Main(string[] args)
{
Thread prodThread = new Thread(ProduceData);
Thread consumeThread = new Thread(ConsumeData);
prodThread.Start();
consumeThread.Start();
Console.ReadLine();
}
private static void ProduceData()
{
for (int i = 0; i < 100; i++)
{
sQ.Enqueue(i.ToString());
}
}
private static void ConsumeData()
{
while (true)
{
if (sQ.Count > 0)
{
string s = sQ.Dequeue();
Console.WriteLine("DEQUEUE::::" + s);
}
}
}
}
Yes you do, System.Collections.Generic.Queue<T>
is not thread safe for being written to and read from at the same time. You either need to lock on the same object before enquing or dequing or if you are using .NET 4/4.5 use the System.Collections.Concurrent.ConcurrentQueue<T>
class instead and use the TryDequeue
method.
The reason your current implementation has not caused you a problem so far, is due to the Thread.Sleep(500)
call (not something you should be using in production code) which means that the prodThread
doesn't write to the queue while the consumeThread
reads from it since the read operation takes less than 500ms. If you remove the Thread.Sleep
odds are it will throw an exception at some point.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With