Another update from ingydotnet^djgoku
This commit is contained in:
parent
91df62d461
commit
948b86eafa
7604 changed files with 108452 additions and 22726 deletions
21
Task/Atomic-updates/Mathematica/atomic-updates.math
Normal file
21
Task/Atomic-updates/Mathematica/atomic-updates.math
Normal file
|
|
@ -0,0 +1,21 @@
|
|||
transfer[bucks_, src_, dest_, n_] :=
|
||||
ReplacePart[
|
||||
bucks, {src -> Max[bucks[[src]] - n, 0],
|
||||
dest -> bucks[[dest]] + Min[bucks[[src]], n]}];
|
||||
DistributeDefinitions[transfer];
|
||||
SetSharedVariable[bucks, comp];
|
||||
bucks = RandomInteger[10, 20];
|
||||
comp = True;
|
||||
Print["Original sum: " <> IntegerString[Plus @@ bucks]];
|
||||
Print[Dynamic["Current sum: " <> IntegerString[Plus @@ bucks]]];
|
||||
WaitAll[{ParallelSubmit[
|
||||
While[True, While[! comp, Null]; comp = False;
|
||||
Module[{a = RandomInteger[{1, 20}], b = RandomInteger[{1, 20}]},
|
||||
bucks = transfer[bucks, Max[a, b], Min[a, b],
|
||||
Floor[Abs[bucks[[a]] - bucks[[b]]]/2]]]; comp = True]],
|
||||
ParallelSubmit[
|
||||
While[True, While[! comp, Null]; comp = False;
|
||||
Module[{src = RandomInteger[{1, 20}],
|
||||
dest = RandomInteger[{1, 20}]},
|
||||
bucks = transfer[bucks, src, dest,
|
||||
RandomInteger[{1, bucks[[src]]}]]]; comp = True]]}];
|
||||
Loading…
Add table
Add a link
Reference in a new issue