Multiple while-loops parallelism

Ali Çehreli acehreli at yahoo.com
Fri Jan 17 18:47:29 PST 2014


On 01/17/2014 01:28 PM, Kelet wrote:

 > create a Task[2]. Once the Task is created, use the executeInNewThread
 > function. You can use yieldForce to wait on it.

That's what I thought initially as well but then I realized that it can 
be even simpler than that:

import std.stdio;
import std.parallelism;

void main()
{
     // All of these loops can be free-standing functions as well

     long sum1 = 0;
     auto loop1 = {
         foreach (number; 0 .. 100) {
             sum1 += number;
         }
     };

     long sum2 = 0;
     auto loop2 = {
         foreach (number; 0 .. 100) {
             sum2 += number;
         }
     };

     foreach (loop; [ loop1, loop2 ].parallel) {
         loop();
     }

     writefln("sum1: %s, sum2: %s", sum1, sum2);
}

Ali



More information about the Digitalmars-d-learn mailing list