Reputation: 2467
I am trying to make it clear for myself what "interlocked" exactly means. I read the following: "The interlocked functions provide a simple mechanism for synchronizing access to a variable that is shared by multiple threads. They also perform operations on variables in an atomic manner."
So could the following functions be called interlocked?
#include <QtCore>
#include <QAtomicPointer>
QAtomicInt i;
void interlockedMultiply(int factor)
{
int oldValue;
do
{
oldValue = i;
} while (!i.testAndSetOrdered(oldValue, oldValue * factor));
}
long long x;
QReadWriteLock lock;
void interlockedAdd(long long y)
{
lock.lockForWrite();
x += y;
lock.unlock();
}
If no, suggest a proper name, please.
Upvotes: 3
Views: 797
Reputation: 98485
It's not deceptive to name these functions as such, but you should still document their semantics, preferably using some sort of a formal description that supports concurrency primitives.
Upvotes: 0
Reputation: 1869
Interlocked means that concurrent operations will yield the expected result. I.e. if you perform an interlocked addition five times, the variable will have been incremented five times. Not more, and not less.
Upvotes: 1