Skip to content

Instantly share code, notes, and snippets.

@pietrom
Created January 12, 2018 14:21
Show Gist options
  • Save pietrom/aa22d4547320a84c1c5df7e16acedc49 to your computer and use it in GitHub Desktop.
Save pietrom/aa22d4547320a84c1c5df7e16acedc49 to your computer and use it in GitHub Desktop.
Concurrent executer, for concurrency-related tests
namespace TestingSupport {
public class ConcurrentExecutor {
public int ThreadCount { get; set; } = 50;
public int RepetitionsCount { get; set; } = 200;
public bool RandomDelayBetweenExecutions { get; set; } = true;
private readonly Random random = new Random();
public void Execute(Action action) {
Barrier startBarrier = new Barrier(ThreadCount);
Barrier endBarrier = new Barrier(ThreadCount + 1);
for (int i = 0; i < ThreadCount; i++) {
new Thread(() => {
startBarrier.SignalAndWait();
for (int j = 0; j < RepetitionsCount; j++) {
if (RandomDelayBetweenExecutions) {
Thread.Sleep(random.Next(10));
}
action();
}
endBarrier.SignalAndWait();
}).Start();
}
endBarrier.SignalAndWait();
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment