Hello,
I'm looking for a way to pass parameters to a function called by a fiber. Starting a fiber works like this:
int main()
{
auto fiber = new Fiber(&myFunc);
fiber.call();
fiber.call();
return 0;
}
void myFunc() {
writeln("Fiber called");
Fiber.yield();
writeln("Fiber recalled after yielding");
}
In the code above there is no way to add parameters to myFunc. The construcor of class Fiber does not allow for a function to be passed that has parameters (unlike for the spawn function when starting a thread).
I ended up with something like this:
class Foo {
public int i;
}
Foo foo;
static this()
{
foo = new Foo;
}
int main()
{
auto fiber = new Fiber(&myFunc);
fiber.call();
fiber.call();
return 0;
}
void myFunc() {
import std.stdio;
writeln("foo: ", foo.i);
foo.i++;
Fiber.yield();
writeln("foo: ", foo.i);
}
But this solution is a bit clumsy. It's kind of programming with global variables.
My question is whether someone has an idea for a better solution.
Thank you, Bienlein