lua-users home
lua-l archive

[Date Prev][Date Next][Thread Prev][Thread Next] [Date Index] [Thread Index]


On 7/9/07, Thomas Lauer <thomas.lauer@virgin.net> wrote:
David Given <dg@cowlark.com> wrote:
> > I am not sure that this line is indeed a problem. Isn't checking an int
> > an atomic operation? As aTask is declared volatile, it should be
> > re-referenced, so a change during the loop would not be a problem
> > either.
>
> Ah, but that's not an atomic operation. That line compiles into something like:

You're right, of course. I was fixed on the comparison (which may indeed
be atomic) but the surrounding pointer arithmetic clearly isn't. I'll
have some more fun with fiddling the mutex then.

There aren't really all that many things that are guaranteed atomic.
The simple comparison is, but even an increment isn't guaranteed. If
you have code that does
foo++;
where foo is shared between threads, you might lose an update. This
despite there being atomic increment machine instructions.

Rule of thumb is all updates are non-atomic, and so are all reads
through a shared pointer, if that pointer moves around.

Cheers,
0K