Hacker News new | ask | show | jobs
by jordigh 3019 days ago
Concurrency isn't the same thing as parallelism. You can have concurrency without ever executing two things at the same time. You just give some CPU cycles to one threaad, then pause it, give cycles to the other thread, and keep switching back and forth. Indeed, this is what any multitasking operating system has always done, even when every computer only had one CPU.

For parallelism as what you describe, D instead has approximately the same thing. There's a `parallel` function in the standard library that works on any range (i.e. any iterable). You basically instead of doing

   foreach(object; somerange) {
      // ..
   }
do

   foreach(object; parallel(somerange)) {
      // ...
   }
and it works like your Perl example.

If you want it more functionalish, without writing out the loop, you can use `taskPool.map` instead of ordinary `map` for the same effect. More details:

stdlib: https://dlang.org/phobos/std_parallelism.html

Programming in D chapter on parallelism: http://ddili.org/ders/d.en/parallelism.html