Anonymous user
Atomic updates: Difference between revisions
m
→{{header|Julia}}
Line 1,885:
=={{header|Julia}}==
<lang julia>using StatsBase
function runall()
spinner = ReentrantLock()
buckets = rand
totaltrans = 0▼
bucketsum() = sum(buckets)
smallpause() = sleep(rand() / 2000
picktwo() = (samplepair(nbuckets)...)
▲ totaltrans = 0
function equalizer()
while
smallpause()
if trylock(spinner)
i, j = picktwo()
sm = buckets[i] + buckets[j]
m =
buckets[i], buckets[j] = m, sm - m
totaltrans += 1
unlock(spinner)
end
end
end
function redistributor()
while
smallpause()
if trylock(spinner)
i, j = picktwo()
sm = buckets[i] + buckets[j]
buckets[i] = rand
buckets[j] = sm - buckets[i]
totaltrans += 1
Line 1,934 ⟶ 1,925:
function accountant()
count = 0
while
smallpause()
if trylock(spinner)
Line 1,943 ⟶ 1,934:
end
end
end
@async equalizer()
@async redistributor()
@async accountant()
while
println("Total transactions: $totaltrans ($
▲ println("Total transactions: $totaltrans ($(Int(round(totaltrans/(time() - t1)))) unlocks per second).")
end
runall()</lang>
{{
<pre>Current state of buckets: [56, 26, 34, 57, 26, 25, 39, 91, 53, 46, 96, 67, 86, 49, 2, 85]. Total in buckets: 838
Current state of buckets: [62, 32, 90, 50, 9, 43, 16, 71, 67, 99, 22, 44, 63, 85, 78, 7]. Total in buckets: 838
Current state of buckets: [58, 25, 41, 30, 9, 79, 42, 43, 32, 66, 110, 123, 90, 35, 13, 42]. Total in buckets: 838
Line 1,974 ⟶ 1,963:
Current state of buckets: [49, 63, 24, 38, 64, 79, 75, 70, 69, 68, 50, 74, 12, 60, 6, 37]. Total in buckets: 838
Current state of buckets: [32, 20, 82, 70, 54, 41, 87, 15, 15, 44, 82, 55, 17, 33, 87, 104]. Total in buckets: 838
Total transactions: 26751 (1639 unlocks per second).</pre>
=={{header|Kotlin}}==
|