[Interest] QMap and thread-safe.

Thiago Macieira thiago.macieira at intel.com
Thu Jul 25 17:34:45 CEST 2013


On quinta-feira, 25 de julho de 2013 13:40:55, Mandeep Sandhu wrote:
> > If that increment was not atomically implemented (and to be honest, right
> > now I don't know whether a "foo++" is atomic - I don't think it is, is
> > it?)
> > and again two threads
> 
> Atomicity of foo++ would depend on the data type of foo, right? Maybe if
> it's a short, the increment will be atomic. Thought the correct way would
> be to use atomic types (or maybe use the gcc atomic extensions).

Incrementing an integer type the size of the machine's word or smaller is 
atomic on x86, but not on most RISC machines (machines with read-modify-update 
incrementing). Incrementing a type that is larger than the machine word, such 
as long long on x86, is not atomic.

Retrieving the value of a type the size of a machine's word or smaller is 
usually atomic everywhere. It is on all architectures that Qt supports.

Doing both at the same time is not atomic anywhere unless you use specialised 
fetch-and-add instructions, which the compiler never generates for you.

I'm being specific because foo++ can be both things and it's impossible to know 
if your code was retrieving the value before the increment:

int inc(int &foo)
{
    return foo++; // not atomic anywhere
}

-- 
Thiago Macieira - thiago.macieira (AT) intel.com
  Software Architect - Intel Open Source Technology Center
-------------- next part --------------
A non-text attachment was scrubbed...
Name: signature.asc
Type: application/pgp-signature
Size: 190 bytes
Desc: This is a digitally signed message part.
URL: <http://lists.qt-project.org/pipermail/interest/attachments/20130725/8b217de9/attachment.sig>


More information about the Interest mailing list