Volatiles & deadlock

classic Classic list List threaded Threaded
3 messages Options
Reply | Threaded
Open this post in threaded view
|

Volatiles & deadlock

Ryan LeCompte

Hello all,

 

Is there any possible way for variables that are declared as “volatile” to possibly get in a deadlock by the JVM? I know that if access to a particular shared mutable variable is protected by methods that are “synchronized”, that it’s still possible for a deadlock to occur depending on the implementations of these synchronized methods. However, if there is nothing to worry about when declaring a variable as volatile and not using the “synchronized” approach to ensure correctness, right?

 

Thanks,

Ryan

 


_______________________________________________
Concurrency-interest mailing list
[hidden email]
http://altair.cs.oswego.edu/mailman/listinfo/concurrency-interest
Reply | Threaded
Open this post in threaded view
|

Re: Volatiles & deadlock

Jeremy Manson
Ryan LeCompte wrote:
> Is there any possible way for variables that are declared as "volatile" to
> possibly get in a deadlock by the JVM? I know that if access to a particular
> shared mutable variable is protected by methods that are "synchronized",
> that it's still possible for a deadlock to occur depending on the
> implementations of these synchronized methods. However, if there is nothing
> to worry about when declaring a variable as volatile and not using the
> "synchronized" approach to ensure correctness, right?

volatile boolean a = false;
volatile boolean b = false;

Thread 1:

1: while (!a);
2: b = true;

Thread 2:

3: while (!b);
4: a = true;

Statement 1 waits for statement 4 waits for statement 3 waits for
statement 2 waits for statement 1: a cycle.

Of course, in this case, regular, non-volatile variables would be just
as likely to deadlock.  Is there a case where volatiles would and normal
variables wouldn't?  No, because volatiles are just like regular
variables, except they have memory consistency guarantees.

                                        Jeremy
_______________________________________________
Concurrency-interest mailing list
[hidden email]
http://altair.cs.oswego.edu/mailman/listinfo/concurrency-interest
Reply | Threaded
Open this post in threaded view
|

Re: Volatiles & deadlock

P.H.Welch
In reply to this post by Ryan LeCompte

Jeremy Manson wrote:

> > Ryan LeCompte wrote:
> > Is there any possible way for variables that are declared as "volatile" to
> > possibly get in a deadlock by the JVM? I know that if access to a particular
> > shared mutable variable is protected by methods that are "synchronized",
> > that it's still possible for a deadlock to occur depending on the
> > implementations of these synchronized methods. However, if there is nothing
> > to worry about when declaring a variable as volatile and not using the
> > "synchronized" approach to ensure correctness, right?
>
> volatile boolean a = false;
> volatile boolean b = false;
>
> Thread 1:
>
> 1: while (!a);
> 2: b = true;
>
> Thread 2:
>
> 3: while (!b);
> 4: a = true;
>
> Statement 1 waits for statement 4 waits for statement 3 waits for
> statement 2 waits for statement 1: a cycle.

Nice example!

Technically, this is livelock though - an even nastier condition than
deadlock.  If theads 1 and 2 were deadlocked, they wouldn't trouble
the processor any more and their inactivity might become obvious (and
debuggable).  In this case, threads 1 and 2 continually trouble the
processor and that unproductive busyness (in a more complex embedding)
might be very un-obvious.  Volatiles scare me ...

Peter.
_______________________________________________
Concurrency-interest mailing list
[hidden email]
http://altair.cs.oswego.edu/mailman/listinfo/concurrency-interest