RE: [Algorithms] Locking without atomic operations
Brought to you by:
vexxed72
From: Kelly B. <kb....@sb...> - 2006-05-31 08:07:21
|
Sorry Carsten, But... > So far the technique (pseudo-code!) > > bool Lock(int id) > { > if(Read(B)!=0){return false;}; > if(Read(A)!=0){return false;}; *** Thread 1 stops here for a timeslice BEFORE writing. > Write(A,id); *** Thread 2 passes this point, doesn't matter where it stops. <Not-important code> If thread 1 stops where I suggest above and thread 2 performs the write, what happens? Thread 1 continues and overwrites what thread 2 just wrote. You now have 2 threads assuming exclusive lock.. End of subject. Ok the fact that the above case can happen 1 in a million times? That's 1 too many times. It "will" happen and I know it way too well from past experience unfortunately because it's a bug I had to track many times. :( Without atomic locks, multiple threads (in SMP systems) can not be guaranteed to behave correctly in any case of writing data. It's really that simple. I challenge anyone to prove that incorrect. Not even incrementing a variable by one is "safe" without CPU level data locking when applied to an SMP environment. KB |