Skip to content

Instantly share code, notes, and snippets.

@Danthar
Created June 18, 2015 10:45
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Danthar/ed3a564396c8214e02fc to your computer and use it in GitHub Desktop.
Save Danthar/ed3a564396c8214e02fc to your computer and use it in GitHub Desktop.
Basic throttler
//Usefull for consumer/producer processing where you keep track of how many work you have served up in the producer
//and call Wait with that amount
//then consumer will call Hit to signal some work has been completed.
//producer will block on Wait until consumer has catched up
public class Throttler {
private AutoResetEvent resetEvent = new AutoResetEvent(false);
private int counter;
private int throttleAmount;
public Throttler(int throttleAmount) {
this.throttleAmount = throttleAmount;
counter = 0;
}
public void Hit() {
counter++;
if (counter == throttleAmount)
resetEvent.Set();
}
public bool Wait(int workDone) {
if (workDone == throttleAmount)
{
resetEvent.WaitOne();
counter = 0;
return true;
}
return false;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment