Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is it safe to put TryDequeue in a while loop?

Tags:

I have not used concurrent queue before.

Is it OK to use TryDequeue as below, in a while loop? Could this not get stuck forever?

var cq = new ConcurrentQueue<string>(); cq.Enqueue("test");  string retValue;  while(!cq.TryDequeue(out retValue)) {     // Maybe sleep? }  //Do rest of code 
like image 298
ManInMoon Avatar asked May 23 '14 14:05

ManInMoon


1 Answers

It's safe in the sense that the loop won't actually end until there is an item it has pulled out, and that it will eventually end if the queue has an item to be taken out. If the queue is emptied by another thread and no more items are added then of course the loop will not end.

Beyond all of that, what you have is a busy loop. This should virtually always be avoided. Either you end up constantly polling the queue asking for more items, wasting CPU time and effort tin the process, or you end up sleeping and therefore not actually using the item in the queue as soon as it is added (and even then, still wasting some time/effort on context switches just to poll the queue).

What you should be doing instead, if you find yourself in the position of wanting to "wait until there is an item for me to take" is use a BlockingCollection. It is specifically designed to wrap various types of concurrent collections and block until there is an item available to take. It allows you to change your code to queue.Take() and have it be easier to write, semantically stating what you're doing, be clearly correct, noticeably more effective, and completely safe.

like image 107
Servy Avatar answered Oct 06 '22 00:10

Servy