Iterating over multiple collections in parallel

downs default_357-line at yahoo.de
Fri Jul 4 04:25:15 PDT 2008


Jarrett Billingsley wrote:
> "BCS" <ao at pathlink.com> wrote in message 
> news:55391cb32ecb08caab0811331728 at news.digitalmars.com...
>> Can a singe thread have more than one stack? If someone can figure out how 
>> to do that, then this wouldn't be "to hard" to do in a lib.
>>
>> call dg1 on first stack, body jumps to stack 2 and calls dg 2, ..., last 
>> dg is called on the threads normal stack and calls the real body.
> 
> Of course, they're called stackthreads or Fibers.  Downs has implemented 
> them in his tools package (in an unstable state) for Phobos, and they've 
> been in Tango for more than two years. 
> 
> 
They're pretty stable now, but they depend on a Phobos patch to run acceptably fast (500 cycles per context switch on my Pentium M).

And I don't know if they run on DMD; there was some problem with the GDC assembler statements. If somebody finds a problem with it, tell me in a reply and I'll try to fix it.

Anyway, here's parallel iteration over two foreachables:

gentoo-pc ~/d $ cat test33.d && echo "----" && rebuild test33.d && ./test33
module test33;
import std.stdio, tools.stackthreads;

void main() {
  auto it1 = [1, 2, 3];
  auto it2 = [1, 4, 9];
  auto iter2 = generator((void delegate(int) yield) {
    foreach (entry; it2) yield(entry);
  });

  foreach (entry1; it1) {
    auto entry2 = iter2();
    writefln(entry1, " - ", entry2);
  }
}
----
1 - 1
2 - 4
3 - 9
gentoo-pc ~/d $



More information about the Digitalmars-d mailing list