I wrote a simple but very multi-threaded prime number generator. The algorithm is as follows: Thread 0: generates consecutive numbers. Topics 1 .. N: filter out numbers that are not first. Each time a new stream is opened, a new filter stream is added.
Take I: no flow control at all. Thread 0 'sends numbers absolutely free. The program ends with signal 11 (fault segregation), rarely signal 8, and even less often ends successfully.
Take II: flow control using 'setMaxMailboxSize' to 1. In most cases, everything works well.
Take III: Now, if all this was the result of some internal unfilled overflow, it should be fine with 'setMaxMailboxSize' to 2 (or even 10), am I wrong? Thread 0 gets stuck after it blocks for the first time.
Can someone please call me what will I miss?
Note 1: I am using DMD v2.053 under Ubuntu 10.04
Note 2: This is my code:
#!/usr/bin/dmd -run import std.stdio; import std.conv; import std.concurrency; void main(string[] args) { if (args.length < 2) { writeln("Usage: prime <number of primes to generate>"); return; } auto nPrimes = to!int(args[1]); auto tid = spawn(&generate, thisTid); for (;;) { auto prime = receiveOnly!int(); writeln(prime); if (--nPrimes <= 0) { break; } } tid.send("stop"); } void generate(Tid parentTid) { bool terminate = false;
source share