L
linuxfedora
if i have a Queue named testQ
Object lockQ = new Object();
testQ will be modified by:
void AddToQueue(string text)
{
lock(lockQ )
{
testQ.Enqueue(text);
}
}
string RemoveFromQueue()
{
lock(lockQ )
{
string text = (string)testQ.Dequeue();
}
return text ;
}
void PrintInfo()
{
lock(lockQ)
{
for(int i=0;i<testQ.Count;i++)
{
Console.WriteLine("String=" + RemoveFromQueue());
}
}
}
How to ensure that the PrintInfo is not affected if when it is
printing the info, a new text is added to the Queue in AddToQueue
function? And the most effective way to do it is?Thanks
Object lockQ = new Object();
testQ will be modified by:
void AddToQueue(string text)
{
lock(lockQ )
{
testQ.Enqueue(text);
}
}
string RemoveFromQueue()
{
lock(lockQ )
{
string text = (string)testQ.Dequeue();
}
return text ;
}
void PrintInfo()
{
lock(lockQ)
{
for(int i=0;i<testQ.Count;i++)
{
Console.WriteLine("String=" + RemoveFromQueue());
}
}
}
How to ensure that the PrintInfo is not affected if when it is
printing the info, a new text is added to the Queue in AddToQueue
function? And the most effective way to do it is?Thanks