Re: Threading - How its done?
Re: Threading - How its done?
- Subject: Re: Threading - How its done?
- From: Michael Vannorsdel <email@hidden>
- Date: Wed, 7 May 2008 22:19:36 -0600
This post contains a lot of great info. However just wanted to clear
up a common misconception on memory barriers. Unfortunately they're
not guaranteed to cause all data in register to be pushed to memory
(don't generate store instructions); only when the program executes a
store instruction is it written out. Memory barriers are to deal with
issues caused by Out-of-Order executing processors like the Core/Core2
and G5. These processors will reorder loads and stores to try and
increase performance. This means a load or store might occur out of
program order. For instance you might have:
x = 5;
y = 2;
With an OOO processor y could be stored before x, regardless of
program order.
What memory barriers do is make sure loads and stores before the
barrier are completed to reflect the program order. So if you do:
x = 5;
y = 2;
OSMemoryBarrier();
You can be sure that if y has been stored, x has as well, assuming the
compiler put stores at those points (why it's important to use
volatile for vars here; without it the compiler might put the store
after the barrier).
This really only matters for multiple OOO processors since the
processor doing out of order loads and stores already knows it and
will make sure everything works as intended. But another processor
might be running code that expects x to be stored before y and it has
no idea if the other processor did these in program order or not.
On May 7, 2008, at 7:13 AM, Army Research Lab wrote:
2) Compilers are smart; they'll keep stuff in registers as long as
possible, including things that are shared between threads. Anything
that is shared, but in a register, won't be seen between threads, so
code
that should be sharing stuff won't be. That is one reason why you MUST
use a lock around all shared variable access, and around any
'sensitive'
code (code that temporarily breaks invariants). Part of the code of
the
lock includes something like OSMemoryBarrier() in
/usr/include/libkern/OSAtomic.h. A memory barrier forces anything
that
is supposed to be in memory to be written all the way out to main
memory
before any further processing happens; that ensures that shared
variables are updated properly.
_______________________________________________
Cocoa-dev mailing list (email@hidden)
Please do not post admin requests or moderator comments to the list.
Contact the moderators at cocoa-dev-admins(at)lists.apple.com
Help/Unsubscribe/Update your Subscription:
This email sent to email@hidden