From: novickivan on
lets say I have

unsigned int x = 0;

And the code

x++;

Is run a total of 1 million times but the million times is divided
across many threads.

If i try to read the value of x am i guaranteed to get a value between
0 and 1 million.

The subtle question here is, are reads and writes of a variable
atomic? Do i have to worry that i am reading the variable while it is
being updated and might get total garbage such as 9 million as the
value when i read it. Or is the worst case that i missed some
increments because the increment on 1 thread was done without taking
into account the updates from another thread?

I hope i have made the question clear.

Thank you,
Ivan Novick
http://www.mycppquiz.com



From: David Schwartz on
On Dec 22, 4:09 pm, "novicki...(a)gmail.com" <novicki...(a)gmail.com>
wrote:
> lets say I have
>
> unsigned int x = 0;
>
> And the code
>
> x++;
>
> Is run a total of 1 million times but the million times is divided
> across many threads.
>
> If i try to read the value of x am i guaranteed to get a value between
> 0 and 1 million.

No, at least not unless some document provides such a guarantee.
Neither the C nor C++ standards provide one.

> The subtle question here is, are reads and writes of a variable
> atomic?  Do i have to worry that i am reading the variable while it is
> being updated and might get total garbage such as 9 million as the
> value when i read it.  Or is the worst case that i missed some
> increments because the increment on 1 thread was done without taking
> into account the updates from another thread?

It depends what the documentation for the threading standard you are
using says. If POSIX, you are not guaranteed anything at all, it could
even crash.

DS
From: Rainer Weikusat on
"novickivan(a)gmail.com" <novickivan(a)gmail.com> writes:
> lets say I have
>
> unsigned int x = 0;
>
> And the code
>
> x++;
>
> Is run a total of 1 million times but the million times is divided
> across many threads.
>
> If i try to read the value of x am i guaranteed to get a value between
> 0 and 1 million.
>
> The subtle question here is, are reads and writes of a variable
> atomic?

The short answer is 'yes'. The longer one would be: This depends on
the machine code which is actually executed and on the properties of
your hardware platform. Provided that x++ is translated to single word
operations and is properly aligned so that it can be loaded and stored
in a single bus cycle, the answer is still yes. If you want a paper
decree regarding 'how people have to generally design hardware', you
won't find one.
From: Rainer Weikusat on
David Schwartz <davids(a)webmaster.com> writes:
> On Dec 22, 4:09�pm, "novicki...(a)gmail.com" <novicki...(a)gmail.com>
> wrote:
>> lets say I have
>>
>> unsigned int x = 0;
>>
>> And the code
>>
>> x++;
>>
>> Is run a total of 1 million times but the million times is divided
>> across many threads.
>>
>> If i try to read the value of x am i guaranteed to get a value between
>> 0 and 1 million.
>
> No, at least not unless some document provides such a guarantee.

A standard is a specification of requirements and as such, doesn't
"guarantee" anything. Insofar a test suite exists and a particular
platform was found to be compliant, that would be sort-of a guarantee
(the test suite itself could be buggy).

[...]

>> The subtle question here is, are reads and writes of a variable
>> atomic? �Do i have to worry that i am reading the variable while it is
>> being updated and might get total garbage such as 9 million as the
>> value when i read it. �Or is the worst case that i missed some
>> increments because the increment on 1 thread was done without taking
>> into account the updates from another thread?
>
> It depends what the documentation for the threading standard you are
> using says. If POSIX, you are not guaranteed anything at all, it could
> even crash.

This is a statement which goes beyond the realm of the standard,
meaning, either it describes an actual example, then this example
should be named, and otherwise, it is science fiction.
From: Rainer Weikusat on
Eric Sosman <esosman(a)ieee-dot-org.invalid> writes:
> On 12/23/2009 10:59 AM, Rainer Weikusat wrote:
>> Eric Sosman<esosman(a)ieee-dot-org.invalid> writes:
>>> On 12/23/2009 8:33 AM, Rainer Weikusat wrote:
>>>> Eric Sosman<esosman(a)ieee-dot-org.invalid> writes:
>>>>> On 12/23/2009 5:24 AM, Rainer Weikusat wrote:
>>>>>> "novickivan(a)gmail.com"<novickivan(a)gmail.com> writes:
>>>>>>> [...]
>>>>>>> The subtle question here is, are reads and writes of a variable
>>>>>>> atomic?
>>>>>>
>>>>>> The short answer is 'yes'.
>>>>>
>>>>> The short answer is "maybe."
>>>>
>>>> That's the long answer, and it shouldn't be 'maybe' but 'very likely
>>>> so' and include a somewhat more detailed explanation. Possibly
>>>> including a list of platforms where the answer is actually no and the
>>>> addresses of the various museums where one can still see them[*].
>>>>
>>>> [*] An example I remembler (from a past posting) was 'some
>>>> m68k platform' where the data bus width was smaller than the
>>>> 'int size'.
>>>>
>>>> BTW, there is no such thing as a good or even honest motive for trying
>>>> to conceal information by threatening others into voluntarily
>>>> blindfolding themselves.
>>>
>>> Thanks for the clarification. I now understand that the
>>> short answer is "maybe."
>>
>> The OP is using Mac OS X on Intel [...]
>
> You made that up.

No I didn't, as you are very well aware. The relevant header which
came with the original posting was

X-HTTP-UserAgent: Mozilla/5.0 (Macintosh; U; Intel Mac OS X 10_5_8; en-US)
AppleWebKit/532.5 (KHTML, like Gecko) Chrome/4.0.249.43 Safari/532.5,gzip(gfe),gzip(gfe)

Not that this would really matter for the core parts of my
statement. So, where are your (or anyone else's) examples for
non-fictional platforms with non-atomic 'int/ unsigned' memory
accesses? This is, after all, information which could be of use, while
hand-waiving generally isn't.