Thread overview
Where can i find examples of multi-threaded fibers?
Jul 26, 2015
Gary Willoughby
Jul 26, 2015
Ali Çehreli
Jul 26, 2015
Gary Willoughby
Jul 27, 2015
Marc Schütz
Jul 27, 2015
Gary Willoughby
Jul 28, 2015
Rob T
July 26, 2015
In the description for Fiber in std.thread is the following[1]:

"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."

How would this be accomplished and are there any code examples available to learn from? I'm assuming this would require a custom scheduler similar to the one in std.concurrency?[2]

[1]: http://dlang.org/phobos/core_thread.html#.Fiber
[2]: http://dlang.org/phobos/std_concurrency.html#.FiberScheduler
July 26, 2015
On 07/26/2015 11:07 AM, Gary Willoughby wrote:> In the description for Fiber in std.thread is the following[1]:
>
> "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."
>
> How would this be accomplished and are there any code examples available
> to learn from? I'm assuming this would require a custom scheduler
> similar to the one in std.concurrency?[2]
>
> [1]: http://dlang.org/phobos/core_thread.html#.Fiber
> [2]: http://dlang.org/phobos/std_concurrency.html#.FiberScheduler

It should be as simple as sending the fiber object between threads (including casting to-and-from shared when needed).

I used std.concurrency.Generator[1][2] below instead of a naked Fiber as it is much more cleaner.

main() uses the fiber then passes it to its worker and continues using it again:

import std.stdio;
import std.range;
import std.algorithm;
import std.concurrency;
import core.thread;

void fiberFunction()
{
    10.iota.each!(i => yield(i));
}

void threadFunction()
{
    auto fiber = cast()receiveOnly!(shared(Generator!int));
    writefln("Produced in worker: %(%s %)", fiber.take(5));
}

void main()
{
    auto numbers = new Generator!int(&fiberFunction);
    auto worker = spawn(&threadFunction);

    writefln("Produced in main  : %(%s %)", numbers.take(2));
    worker.send(cast(shared)numbers);
    thread_joinAll();

    writefln("Produced in main  : %(%s %)", numbers);
}

Here is the output:

Produced in main  : 0 1
Produced in worker: 2 3 4 5 6
Produced in main  : 7 8 9

Ali

[1] http://dlang.org/phobos/std_concurrency.html#.Generator
[2] http://ddili.org/ders/d.en/fibers.html#ix_fibers.Generator,%20std.concurrency

July 26, 2015
On Sunday, 26 July 2015 at 19:51:08 UTC, Ali Çehreli wrote:
> On 07/26/2015 11:07 AM, Gary Willoughby wrote:> In the

Thanks for the example. I'll study it.


July 27, 2015
On Sunday, 26 July 2015 at 18:07:51 UTC, Gary Willoughby wrote:
> In the description for Fiber in std.thread is the following[1]:
>
> "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."
>
> How would this be accomplished and are there any code examples available to learn from? I'm assuming this would require a custom scheduler similar to the one in std.concurrency?[2]
>
> [1]: http://dlang.org/phobos/core_thread.html#.Fiber
> [2]: http://dlang.org/phobos/std_concurrency.html#.FiberScheduler

There was a long discussion about this a few weeks ago, and IIRC the outcome was that we shouldn't allow it, because it breaks the type system.
July 27, 2015
On Monday, 27 July 2015 at 08:00:10 UTC, Marc Schütz wrote:
> On Sunday, 26 July 2015 at 18:07:51 UTC, Gary Willoughby wrote:
>> In the description for Fiber in std.thread is the following[1]:
>>
>> "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."
>>
>> How would this be accomplished and are there any code examples available to learn from? I'm assuming this would require a custom scheduler similar to the one in std.concurrency?[2]
>>
>> [1]: http://dlang.org/phobos/core_thread.html#.Fiber
>> [2]: http://dlang.org/phobos/std_concurrency.html#.FiberScheduler
>
> There was a long discussion about this a few weeks ago, and IIRC the outcome was that we shouldn't allow it, because it breaks the type system.

Where is the discussion?
July 28, 2015
On Monday, 27 July 2015 at 18:30:06 UTC, Gary Willoughby wrote:
> Where is the discussion?

This must be it
http://forum.dlang.org/thread/pflkijjjuyyhextxvdnn@forum.dlang.org

I'm interested in the same subject right now, and was wondering about the same question you asked.