Jump to content

Atomic updates: Difference between revisions

m
Line 1,885:
 
=={{header|Julia}}==
<lang julia>using StatsBase
 
function runall()
bucketsizenbuckets = 16
unfinishedunfinish = true
spinner = ReentrantLock()
buckets = rand(collect(1:99), bucketsizenbuckets)
totaltrans = 0
 
bucketsum() = sum(buckets)
smallpause() = sleep(rand() / 2000.)
picktwo() = (samplepair(nbuckets)...)
totaltrans = 0
function picktwo()
i, j = rand(collect(1:bucketsize), 2)
if i == j
if i > 2
i -= 1
else
i += 1
end
end
i, j
end
function equalizer()
while unfinishedunfinish
smallpause()
if trylock(spinner)
i, j = picktwo()
sm = buckets[i] + buckets[j]
m = Int(floor(fld(sm + 1)/, 2.))
buckets[i], buckets[j] = m, sm - m
buckets[j] = sm - buckets[i]
totaltrans += 1
unlock(spinner)
end
end
end
function redistributor()
while unfinishedunfinish
smallpause()
if trylock(spinner)
i, j = picktwo()
sm = buckets[i] + buckets[j]
buckets[i] = rand(collect(0:sm))
buckets[j] = sm - buckets[i]
totaltrans += 1
Line 1,934 ⟶ 1,925:
function accountant()
count = 0
while( count < 16)
smallpause()
if trylock(spinner)
Line 1,943 ⟶ 1,934:
end
end
unfinishedunfinish = false
end
t1t = time()
@async equalizer()
@async redistributor()
@async accountant()
while unfinishedunfinish sleep(0.25) end
println("Total transactions: $totaltrans ($(Int(round(Int, totaltrans / (time() - t1)t))) unlocks per second).")
sleep(0.25)
end
println("Total transactions: $totaltrans ($(Int(round(totaltrans/(time() - t1)))) unlocks per second).")
end
 
runall()</lang>
 
</lang>
{{outputout}}<pre>
<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>
</pre>
 
=={{header|Kotlin}}==
Anonymous user
Cookies help us deliver our services. By using our services, you agree to our use of cookies.