Talk:Atomic updates: Difference between revisions

(Transfer is clarified)
 
(3 intermediate revisions by 3 users not shown)
Line 14:
:::::Assume that this entire routine is executed atomically. It preserves both invariants -- the sum of buckets (by changing two buckets by equal and opposite amounts) and that each bucket is positive (by clamping the amount transferred). Why can't you do the same thing in Ada? --[[User:Kevin Reid|Kevin Reid]] 18:48, 18 May 2009 (UTC)
::::::Ah, finally I've got it. Thanks for an explanation. That is no problem. Can you move this definition of transfer into the task description? E.g. "takes two indices and then adds a specified amount to the bucket indexed by one and subtracts it from one indexed by another index, but no more than its total value." One question remains, how can this transfer be used to make pairs of buckets equal (averaging)? You need an atomic read + decide + update operation. Or this is not required by the task? --[[User:Dmitry-kazakov|Dmitry-kazakov]] 19:24, 18 May 2009 (UTC)
:::::::I've tried to clarify the description. And yes, checking the current value is explicitly non-atomic. The idea is that in the absence of disturbance, the neatening task ''would'' achieve its goal; each step is, on average, an improvement, unless there is another task specifically perversely adjusting the buckets just in time. This isn't intended to be a sensible example of how to accomplish anything on a small data set; it's intended to be solely an exercise in preserving invariants despite sloppy/buggy/competing clients. --[[User:Kevin Reid|Kevin Reid]] 19:39, 18 May 2009 (UTC)
 
== And in a language that does not support multithreading ? ==
The question is in the title. While preserving an invariant still makes sense, the task needs some clarification: in that case, it doesn't illustrate anything about atomic updates, since there cannot be multiple updates at the same time anyway. [[User:Capra Hircus|Capra Hircus]] 19:53, 1 September 2012 (UTC)
 
== Go code ==
For people that want to actually try to run the code, I think the Go entries should explain that to compile the Go entries you need some changes:
<lang go>import (
"fmt"
"math/rand"
"time"
"sync"
"runtime"
)
...
func main() {
// Create a concrete object implementing the bucketList
interface.
bl := newRwList(10, originalTotal, nUpdaters)</lang>
 
And for the third:
 
<lang go>import (
"fmt"
"math/rand"
"time"
"sync"
"runtime"
"sync/atomic"
)
...
func main() {
// Create a concrete object implementing the bucketList
interface.
bl := newLfList(10, originalTotal, nUpdaters)</lang>
 
The first Go solution compiles as it is, but I don't see any printing output.