I am writing an queue processing application which uses threads for waiting on and responding to queue messages to be delivered to the app. For the main part of the applica
You don't give much context to what you are really doing, but maybe Queue could be used instead of an explicit busy-wait loop? If not, I would assume sleep
would be preferable, as I believe it will consume less CPU (as others have already noted).
[Edited according to additional information in comment below.]
Maybe this is obvious, but anyway, what you could do in a case where you are reading information from blocking sockets is to have one thread read from the socket and post suitably formatted messages into a Queue
, and then have the rest of your "worker" threads reading from that queue; the workers will then block on reading from the queue without the need for neither pass
, nor sleep
.