Share array element between threads

Misu via Digitalmars-d-learn digitalmars-d-learn at puremagic.com
Thu Nov 6 02:53:31 PST 2014


Hi,

when I execute this code I have 7 7 7 as result, I think I 
understand why.

How can I execute a special task for one element ?

import std.stdio;
import std.parallelism;

void main(string[] args)
{
	class Account
	{
		public this(int id) { this.id = id; }
		int id;
	}

	Account[] accounts = [new Account(5), new Account(6), new 
Account(7)];

	foreach(acc; accounts)
	{
		task(() { writeln(acc.id); }).executeInNewThread();
	}
	
	readln();
}


More information about the Digitalmars-d-learn mailing list