Simone Grignola
Salvatore Capuano
Board International SA
Based on Sasha Goldshtein session @ DevReach 2012
try //The C# 5.0 version
{
var task = Task.Run(DnaSimulation);
DisplayProgress();
Show(await task);
}
catch(Exception ex)
{
Show(ex);
}
Parallel.For(0, 100, i => {
...
});
Parallel.ForEach(urls, url => {
webClient.Post(url, options, data);
});
Parallel.ForEach(
Partitioner.Create(Start, End, ChunkSize),
() => List(), //initial local state
(range, pls, localPrimes) => { //aggregator
for (int i = range.Item1; i < range.Item2; ++i)
if (IsPrime(i)) localPrimes.Add(i);
return localPrimes;
},
localPrimes => { //combiner
lock (primes)
primes.AddRange(localPrimes);
});
GET(key):
if safe contains key then
return safe[key]
lock
{
return unsafe[key]
}
PUT(key, value):
if safe contains key then safe[key] = value
lock
{
unsafe[key] = value
}
int
InterlockedMultiply(
ref
int
x
,
int
y
) {
int
t, r;
do
{
t =
x
;
r = t *
y
;
}
while
(Interlocked.CompareExchange(
ref
x
, r, t) != t);
return
r;
}