.NET Framework 4.0 の Parallel Extensions(並列プログラミング)を使うと、マルチスレッドなプログラムが簡単に作れるんですね。
以下は、重たい処理を 2 回実行する従来の処理方法です。
日本語でメソッドっぽくないけど、メソッドですw
重い処理(0);
重い処理(1);
お馴染みの書き方ですね。
重たい処理(); が終わると、次の重い処理(); が実行されます。
using System.Threading.Tasks;
:
Task t0 = Task.StartNew(() => 重い処理(0));
Task t1 = Task.StartNew(() => 重い処理(1));
こう書くと、最初の重い処理の終了を待たずに、次の重い処理が実行されるらしいです。
簡単だなー。
for や foreach や LINQ などでも、並列処理が可能です。
通常
for (int i = 0; i < 10; ++i) 重たい処理(i);
foreach(var x in collection) 重たい処理(x);
var q = from x in collection select x;
並列
Parallel.For(0, 10, i => 重たい処理(i));
Parallel.Foreach(collection, x => 重たい処理(x));
var q = from x in collection.AsParallel() select x;
素晴しき並列処理
実装処理も、それを実現するテクノロジも全く違うものですが、これらを使って、従来の構文に似せることで敷居をさげているのですね。
#ちなみにシングルコアなプロセッサだと、何か意味があるのだろうか?