Created
June 18, 2015 10:45
-
-
Save Danthar/ed3a564396c8214e02fc to your computer and use it in GitHub Desktop.
Basic throttler
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
//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