Jump to content

Atomic updates: Difference between revisions

m
(Add Common Lisp)
Line 1,883:
 
Commenting out either of the threads mutating the buckets shows that they work properly.
 
=={{header|Julia}}==
<lang julia>
function runall()
bucketsize = 16
unfinished = true
spinner = ReentrantLock()
buckets = rand(collect(1:99), bucketsize)
bucketsum() = sum(buckets)
smallpause() = sleep(rand()/10.)
function picktwo()
i, j = rand(collect(1:bucketsize), 2)
while (i == j)
i, j = picktwo()
end
i, j
end
function equalizetwo()
i, j = picktwo()
sm = buckets[i] + buckets[j]
m = Int(floor((sm + 1)/2.))
buckets[i] = m
buckets[j] = sm - buckets[i]
end
function redistributetwo()
i, j = picktwo()
sm = buckets[i] + buckets[j]
if buckets[i] != 0
buckets[i] = rand(collect(0:sm))
buckets[j] = sm - buckets[i]
end
end
function equalizer()
while unfinished
smallpause()
if trylock(spinner)
equalizetwo()
unlock(spinner)
end
end
end
function redistributor()
while unfinished
smallpause()
if trylock(spinner)
redistributetwo()
unlock(spinner)
end
end
end
function accountant()
count = 0
while(count < 16)
smallpause()
if trylock(spinner)
println("Current state of buckets: $buckets. Total in buckets: $(bucketsum())")
unlock(spinner)
count += 1
sleep(0.5)
end
end
unfinished = false
end
@async equalizer()
@async redistributor()
@async accountant()
while unfinished
sleep(0.25)
end
end
 
runall()
</lang>
{{output}}<pre>
Current state of buckets: [65, 65, 49, 26, 47, 41, 53, 49, 13, 1, 76, 75, 59, 27, 92, 89]. Total in buckets: 827
Current state of buckets: [86, 66, 31, 14, 51, 66, 51, 49, 54, 47, 44, 45, 60, 30, 92, 41]. Total in buckets: 827
Current state of buckets: [30, 1, 83, 47, 41, 75, 47, 44, 39, 31, 82, 50, 44, 40, 129, 44]. Total in buckets: 827
Current state of buckets: [74, 83, 110, 4, 24, 41, 83, 110, 12, 47, 27, 39, 2, 25, 70, 76]. Total in buckets: 827
Current state of buckets: [24, 72, 51, 47, 22, 72, 83, 38, 42, 38, 46, 46, 40, 59, 84, 63]. Total in buckets: 827
Current state of buckets: [27, 61, 51, 8, 66, 110, 104, 64, 69, 30, 18, 8, 66, 46, 45, 54]. Total in buckets: 827
Current state of buckets: [92, 51, 8, 9, 180, 9, 23, 52, 97, 30, 23, 8, 66, 26, 50, 103]. Total in buckets: 827
Current state of buckets: [63, 142, 45, 63, 69, 44, 38, 44, 52, 48, 45, 8, 63, 26, 50, 27]. Total in buckets: 827
Current state of buckets: [50, 119, 68, 69, 24, 41, 16, 12, 119, 36, 67, 10, 48, 21, 50, 77]. Total in buckets: 827
Current state of buckets: [46, 16, 54, 6, 168, 47, 5, 47, 54, 7, 149, 25, 30, 28, 36, 109]. Total in buckets: 827
Current state of buckets: [33, 37, 41, 45, 62, 45, 46, 53, 42, 85, 50, 25, 70, 41, 65, 87]. Total in buckets: 827
Current state of buckets: [33, 20, 33, 54, 49, 4, 56, 33, 52, 84, 50, 103, 96, 83, 49, 28]. Total in buckets: 827
Current state of buckets: [33, 41, 62, 67, 49, 47, 63, 59, 31, 67, 84, 34, 50, 62, 47, 31]. Total in buckets: 827
Current state of buckets: [133, 3, 62, 11, 2, 90, 21, 94, 62, 88, 52, 58, 4, 18, 88, 41]. Total in buckets: 827
Current state of buckets: [44, 3, 90, 21, 2, 43, 11, 94, 74, 44, 52, 100, 40, 30, 105, 74]. Total in buckets: 827
Current state of buckets: [60, 15, 68, 10, 2, 67, 12, 94, 48, 66, 72, 73, 65, 69, 65, 41]. Total in buckets: 827
</pre>
 
=={{header|Lasso}}==
Line 1,971 ⟶ 2,062:
staticarray(281, 717, 341, 716, 50, 17, 129, 247, 964, 197) -- total: 3659
staticarray(423, 509, 51, 458, 265, 423, 292, 458, 661, 119) -- total: 3659</pre>
 
 
=={{header|Logtalk}}==
4,105

edits

Cookies help us deliver our services. By using our services, you agree to our use of cookies.