R
Ryan Liu
Hi,
If I have many threads write to a variable(e.g. var++) and another thread
read it on an interval base.
For those writing thread, I know I need lock, or its value could be lower
( even I think it is mostly not going to happen for ++ operation since it is
not something like read a value and wait sometime then write back in
multiple threading environment, BTW, is this understanding right?).
My question, for the reading thread, do I need lock?
If a lock is still a must, what about when I can accept race condition,
just the read value don't to be "too old", e.g. 2 or more less than the its
current value. And new read should never less then old read.
If I don't use lock in read thread, do I have to use volatile for the var,
otherwise it has the potential not to read the current value?
Thanks a lot!
If I have many threads write to a variable(e.g. var++) and another thread
read it on an interval base.
For those writing thread, I know I need lock, or its value could be lower
( even I think it is mostly not going to happen for ++ operation since it is
not something like read a value and wait sometime then write back in
multiple threading environment, BTW, is this understanding right?).
My question, for the reading thread, do I need lock?
If a lock is still a must, what about when I can accept race condition,
just the read value don't to be "too old", e.g. 2 or more less than the its
current value. And new read should never less then old read.
If I don't use lock in read thread, do I have to use volatile for the var,
otherwise it has the potential not to read the current value?
Thanks a lot!