Skip to content

Instantly share code, notes, and snippets.

@alphaKAI
Created February 9, 2015 16:48
Show Gist options
  • Select an option

  • Save alphaKAI/c4c57a1a17c357ad2956 to your computer and use it in GitHub Desktop.

Select an option

Save alphaKAI/c4c57a1a17c357ad2956 to your computer and use it in GitHub Desktop.
昨日の再帰FizzBuzzをマルチスレッドで実行する奴。 fizzbuzzOrderで求めたいfizzbuzzの最後の値を指定し、bitSizeで1スレッドでの計算数を指定できる。
import std.conv,
std.stdio,
std.range,
std.algorithm;
import core.thread;
string[] fizzbuzz(int max, int i = 0, string[] base = null){
return max == 0 ? base : fizzbuzz(max - 1, ++i, base ~ {
return !(i % 15) ? "FizzBuzz" :
!(i % 3) ? "Fizz" :
!(i % 5) ? "Buzz" : i.to!string;
}());
}
void main(){
immutable fizzbuzzOrder = 30000;
immutable bitSize = 50;
immutable threadMax = fizzbuzzOrder / bitSize;
string[fizzbuzzOrder] threadVals;
int threadCount;
foreach(i; 0..(fizzbuzzOrder / bitSize))
new Thread((){
threadVals[(bitSize * i)..(bitSize * (i + 1))] = fizzbuzz(bitSize, bitSize * i);
threadCount++;//Add finished thread
}).start;
while(threadCount < (fizzbuzzOrder / bitSize)){}//Wait
foreach(e; threadVals)
e.writeln;
writeln("len:", threadVals.length);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment