Atomicity Question

  • Thread starter Thread starter Cool Guy
  • Start date Start date
C

Cool Guy

In one thread, I do something like: MyObject myObject = new MyObject();

Can I now do the following succesfully in another thread?

if (myObject != null) {
// access myObject
}
 
ooh aarf....
now that you ask....

mmhh....
in your case doesn't matter it wouldjust be null a little bit longer!

and it might be write back immediately if all you do in the other thread is
allocating it!
volatile value might simply not be updated if they change a few time in the
procedure, in which case th code generator would just use registry and
commit only the last value
 
Actually -- come to think of it, I need to *lock* instead (i.e. not use a
volatile variable) in the code which prompted me to ask this question,
anyway.
Not necessarily. You either need to make the variable volatile, or (my
preferred route) always access shared data from within a lock.

Hmm... I'm beginning to see why you prefer to always use locking. :-)
 
I said:
Actually -- come to think of it, I need to *lock* instead (i.e. not use a
volatile variable) in the code which prompted me to ask this question,
anyway.

It's quite obvious to me now that in the code in question I had to use
locking, but when I first realised there might be a problem my brain was
working so hard that I was 'blind' to this.

I hate multi-threading. :-(
 
Back
Top