Hello Lazarus-List, Tuesday, June 28, 2011, 6:44:17 PM, you wrote:
I can only talk about Win x86, so things could be different in other OS. AB> Your assumption would be incorrect. The system doesn't do anything AB> but decrease the spincount of the criticalsection object to make sure AB> the variable is valid. As stated before by numerous people, a AB> CriticalSection does not apply to variable assignments. It just AB> blocks entrance to code blocks unit the lock is released. In Win a Critical section do more things than increase, decrease the recursion count, if first try to detect the Critical adquisition using a memory barrier/atomic (exchange). If adquire fails it "spins", not exactly spins but valid for this text, until it success. The try to adquire imposes a non reorder and cache coherence across threads, cores and processors. As much the critical spin to lock the critical section CPU performance is degradated due constantly memory fences. *** This will never work as thread 2 does not use the protection mechanism expected by thread 1. Thread/core/processor 1 ----------------------- while true do begin Critical.Enter; a:=a+1; b:=a; Critical.Leave; end; Thread/core/processor 2 ----------------------- while true do begin if a<>b then Raise Exception.Create('KBOOM!'); end; *** This one must work Thread/core/processor 2 ----------------------- while true do begin Critical.Enter; if a<>b then Raise Exception.Create('KBOOM!'); Critical.Leave; end; If the second one will not work in a n-code processor, or SMP, please tell me why not. Of course the performance will be quite poor. -- Best regards, José -- _______________________________________________ Lazarus mailing list Lazarus@lists.lazarus.freepascal.org http://lists.lazarus.freepascal.org/mailman/listinfo/lazarus