// Assume a hot loop worth optimizing.
while (...)
{
...
// Protect access to x if multithreaded.
if (bThreaded) oLock.lock ();
// Do a lot of work with x here.
x = f (x);
// Protect access to x if multithreaded.
if (bThreaded) oLock.unlock ();
...
}
// Since x is used a lot it is kept in some register.
register = x;
while (...)
{
...
if (bThreaded)
{
// External call may use x so it is written back.
x = register;
oLock.lock ();
register = x;
}
// A lot of work done efficiently with x in register.
register = f (register);
if (bThreaded)
{
// External call may use x so it is written back.
x = register;
oLock.unlock ();
register = x;
}
...
}
x = register;
Example adjusted from literature, see references.