Skip to content

Instantly share code, notes, and snippets.

@JesseKPhillips
Last active September 24, 2015 16:18
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save JesseKPhillips/774983 to your computer and use it in GitHub Desktop.
Save JesseKPhillips/774983 to your computer and use it in GitHub Desktop.
Example of callcenter using parrallelfuture
module main;
import core.thread;
import std.algorithm;
import std.conv;
import std.datetime;
import std.parallelism;
import std.random;
import std.range;
import std.stdio;
struct Call {
int id;
Duration duration;
SysTime start_time;
}
auto callGenerator(alias Duration max_dur, alias Duration max_int)(int i) {
Thread.sleep(max_int);
return Call(i, hnsecs(uniform(1, max_dur.fracSec.hnsecs)), Clock.currTime);
}
void main(string[] args) {
if(args.length < 5) {
writeln("Usage: CallGenerator <numberOfAgents> <numberOfCalls> <maxCallDuration> <maxCallInterval>");
return;
}
int num_agents = to!int(args[1]);
int num_calls = to!int(args[2]);
auto max_dur = hnsecs(to!int(args[3]));
auto max_int = hnsecs(to!int(args[4]));
auto pool = new TaskPool(num_agents);
auto begin = Clock.currTime();
alias callGenerator!(max_dur, max_int) generate;
auto calls = map!((int call) { return generate(call); })(iota(0, num_calls));
foreach(call; pool.parallel(calls)) {
synchronized writefln("Answering Call %s; waited %s seconds\n", call.id, (Clock.currTime - call.start_time));
Thread.sleep(call.duration * 10_000_000);
synchronized writefln("Call %s answered; took %s seconds\n", call.id, call.duration);
}
pool.stop();
auto end = Clock.currTime();
writefln("Simulation elapsed time %s seconds", (end-begin).toString);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment