Round robbin

by david 25. September 2009 14:14

I was doing some simple load balancing tests on an application I'm building.  I wanted to load multiple queues from different places in my code using a simple round-robin approach.  A very simple thing to do, and this is what I came up with:

 

int index = 0, max = 10;

...

index += index >= max ? (index * -1) : 1;

 

...it will increment the index counter until it reaches the limit (10), then start back at zero again. I don't know why I find that line so beautiful, but I just do.  I like it when you can do stuff in a single line of code.

About the author

David
I'm a C# developer having worked with .Net since it was in beta.  Before that I mainly worked in C and C++.  I have been developing commercial software for more than 20 years.  I also mess around with microprocesors, but that's just for fun.  I live near Cambridge, England and at the moment I'm contracted to one of the departments at Cambridge University.