Strange behavior with 'setMaxMailboxSize'

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) { /* parse command line arguments */ if (args.length < 2) { writeln("Usage: prime <number of primes to generate>"); return; } auto nPrimes = to!int(args[1]); auto tid = spawn(&generate, thisTid); /* gather produced primes */ for (;;) { auto prime = receiveOnly!int(); writeln(prime); if (--nPrimes <= 0) { break; } } tid.send("stop"); } void generate(Tid parentTid) { bool terminate = false; // filter stage 1 auto tid = spawn(&filter_stage, parentTid); /* WHAT DO I MISS HERE ? */ setMaxMailboxSize(tid, 1, OnCrowding.block); for (int i = 2; !terminate; i++) { receiveTimeout(0, (string cmd) { writeln(cmd); terminate = true; } ); tid.send(i); } } void filter_stage(Tid parentTid) { auto prime = receiveOnly!int(); parentTid.send(prime); // filter stage 'N' auto tid = spawn(&filter_stage, parentTid); filter(prime, tid); } void filter(int prime, Tid tid) { for (;;) { receive ( (int number) { if (number % prime != 0) { tid.send(number); } } ); } } 
+4
source share
1 answer

Sounds like an error in std.concurrency. Try updating your DMD to 2.055. I am not sure if this bug has been fixed, but there are many bug fixes between 2.053 and 2.055. If it is still broken, please write a bug report at http://d.puremagic.com/issues/ .

+2
source

All Articles