2

I am wondering how I do I pass a fiber to a thread?

The only way that I managed to do it was by casting to and from shared.

auto fiber = new Fiber((){
});
auto t = spawn((){
    auto fib = cast(Fiber)receiveOnly!(shared(Fiber));
    writeln("fib");
    fib.call();

});
send(t, cast(shared(Fiber))fiber);

But that doesn't seem right. I think I don't fully understand the implications of shared.

I had to cast the fiber to shared because I wasn't allowed to send it to the thread without it. I had to cast it back to a fiber because I couldn't call Fiber.call on a shared fiber.

What is the correct way of passing a Fiber to a thread?

Maik Klein
  • 15,548
  • 27
  • 101
  • 197

1 Answers1

3

There is no support for moving fibres across threads, right now (and probably will never be). Using shared could somehow works, but it is dangerous, and I am not sure if it will work ok with all compilers. Some more info is in this dlang forum thread:

http://forum.dlang.org/post/m2r3prh3ki.fsf@comcast.net

Kozzi11
  • 2,413
  • 12
  • 17
  • Is the documentation wrong? "Please note that there is no requirement that a fiber be bound to one specific thread. Rather, fibers may be freely passed between threads so long as they are not currently executing." – Maik Klein Apr 16 '16 at 12:28
  • It is not wrong (right now), because you can still use shared(Fiber) with all implications, but it breaks D typesystem so it is possible that it will be dissallowed in future. – Kozzi11 Apr 16 '16 at 13:27
  • Documentation was written like that because originally Walter wanted to supported it. However some major existing commercial D users (Sociomantic and weka.io) are very opposed to it and there is a strong push to change spec to ban it forever. – Mihails Strasuns Apr 16 '16 at 13:47
  • 1
    You shouldn't move fibers between threads. If you need to do such thing then perhaps you're approaching your task wrong. – Bauss Apr 16 '16 at 18:03