[Interest] QMap and thread-safe.

Mandeep Sandhu mandeepsandhu.chd at gmail.com
Fri Jul 26 06:57:28 CEST 2013


On Thu, Jul 25, 2013 at 9:04 PM, Thiago Macieira
<thiago.macieira at intel.com>wrote:

> 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.
>

By both here, you mean fetch AND add, right? And in x86 such a operation
(add) on a machine word is atomic w/o using special instructions?

Eg if I had code like this running on a x86 machine:

short foo;
foo++;

Will the increment be atomic or do I have to explicitly make it so by doing
something extra?


> 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
>

Why? Because of the way foo is being received in this function, i.e by
reference (so any operation on foo has and added level of indirection)?

Thanks,
-mandeep

}
>
> --
> Thiago Macieira - thiago.macieira (AT) intel.com
>   Software Architect - Intel Open Source Technology Center
>
> _______________________________________________
> Interest mailing list
> Interest at qt-project.org
> http://lists.qt-project.org/mailman/listinfo/interest
>
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.qt-project.org/pipermail/interest/attachments/20130726/fd35d683/attachment.html>


More information about the Interest mailing list