xxxxxxxxxx
import std.stdio;
import core.thread;
import std.concurrency;
shared struct IdGen(T)
{
T id;
this(T start)
{
id = start;
}
T next()
{
id = id.next();
return id;
}
}
struct MyId
{
uint num;
shared MyId next()
{
return MyId(num + 1);
}
}
static void getId(shared IdGen!(MyId)* g)
{
writeln("next: ", g.next());
writeln("next: ", g.next());
}
void main()
{
MyId start = {0};
auto g = IdGen!(MyId)(start);
auto num = 12;
for (auto i = 0; i < num; i++)
{
spawn(&getId, &g);
}
thread_joinAll();
writeln("Done");
}