Multiple while-loops parallelism

Kelet kelethunter at gmail.com
Fri Jan 17 13:28:28 PST 2014


On Friday, 17 January 2014 at 21:07:46 UTC, Mineko wrote:
> Today I'm asking a more theoretical question, since I can't 
> quite grasp this one too well.
>
> Let's say I want 3 while-loops running in parallel, without 
> getting in the way of each other, how would I do that?
>
> With std.parallel of course, but that's where I get confused, 
> perhaps someone could enlighten me?

Hi,

> std.parallel
You mean std.parallelism.

Assuming your code is thread safe[1], you would have each of
these while loops in a delegate or function, and create a
Task[2]. Once the Task is created, use the executeInNewThread
function. You can use yieldForce to wait on it. However, if you
don't *really* need while loops, take a look at creating a
TaskPool and using a parallel foreach, reduce, map, etc. They are
also in std.parallelism.

However, it's worth noting that there is also std.concurrency[3]
which may be a better approach if your threads need to
communicate. core.thread/core.atomic are useful if you need lower
level control.

If your code is not thread safe, look into synchronized
statement, core.atomic, and possibly core.sync.* and make it
thread safe.

[1]: http://en.wikipedia.org/wiki/Thread_safety
[2]: http://dlang.org/phobos/std_parallelism.html#.Task
[3]: http://dlang.org/phobos/std_concurrency.html

Regards,
Kelet


More information about the Digitalmars-d-learn mailing list