Win a copy of Murach's Python Programming this week in the Jython/Python forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

is multiple read of a volatile variable possible?  RSS feed

 
s ravi chandran
Ranch Hand
Posts: 547
5
Java jQuery
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,
I had an interview recently, and the interviewer was focussing mainly on multithreading. I gave almost all the answers as far as I know, I was right on most. But there was one question where I got confused. The question is like this. there are two threads, which are updating a variable only if its equal to 10.. now thread1 will check the variable, and if it is 10, this will make it 20. thread2 will check the variable, and if its 10, it will make the variable 30.

now we run both the threads, what should be the result.
i told him, considering thread1 runs first, it shd change the value as 20 and it will reflect to thread2 also. because with volatile variables, changes are reflected to the main memory instantly.

but he said, no, if i have many cores in processor, there will be a scenario where both threads read the value 10 at the same time. this is where i am confused..

in multiprocessor scenario, is this case possible?

Thanks
 
Ulf Dittmer
Rancher
Posts: 42970
73
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Both are possible - it depends on how you implement it. If you post specific code we can talk about how that might behave.
 
s ravi chandran
Ranch Hand
Posts: 547
5
Java jQuery
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,
sorry for the delay. well, from what i remember the description, here is the code flow:

 
Henry Wong
author
Sheriff
Posts: 22851
119
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
s ravi chandran wrote:
in multiprocessor scenario, is this case possible?


Yes. It is possible. In fact, it is possible with a single processor scenario too.

s ravi chandran wrote:


Keep in mind that the actions of this code is not atomic. So, if one thread looks at the count variable, compares it to ten, succeeds, but, before it can set it to the new value, it takes another context switch. Later, another thread looks at the count variable, compares it to ten, succeeds, and then sets it to it new value. Even later, the first thread runs again, and sets it to it's new value.

Both threads check the count variable, confirmed that it was ten, and set it to the new value.

Henry
 
Jelle Klap
Bartender
Posts: 1952
7
Eclipse IDE Java
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
This is in fact an explicit point made in Java Concurrency in Practice (recommended read):

You can use volatile variables only when all the following criteria are met:
- Writes to the variable do not depend on its current value, or you can ensure that only a single thread ever updates the value;
<omitted>

Volatile only guarentees the visibility aspect, but does not imply atomicity, as Henry said, because it does not enforce mutual exclusion like a lock would.
 
s ravi chandran
Ranch Hand
Posts: 547
5
Java jQuery
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Jelle Klap wrote:This is in fact an explicit point made in Java Concurrency in Practice (recommended read):

You can use volatile variables only when all the following criteria are met:
- Writes to the variable do not depend on its current value, or you can ensure that only a single thread ever updates the value;
<omitted>

Volatile only guarentees the visibility aspect, but does not imply atomicity, as Henry said, because it does not enforce mutual exclusion like a lock would.


but assignment operation should be atomic right?
 
Rahul Mahindrakar
Ranch Hand
Posts: 1869
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Intresting

Thread T1 can change 10 to 20

and Thread T2 can change 10( which has now become 20) to 30.

Rahul
 
Rahul Mahindrakar
Ranch Hand
Posts: 1869
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator


I had to change the code to remove the check for the variable and move it to the threads to make it more explicit.

Now both the threads see a 10

Here is the output

T1 10
T2 10
T1 20
T2 30

The end result I expected was either a 20 or a 30 but not a 20 and 30.


 
Rahul Mahindrakar
Ranch Hand
Posts: 1869
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi

I changed the code due to the following in the first discussion

now thread1 will check the variable, and if it is 10, this will make it 20. thread2 will check the variable, and if its 10, it will make the variable 30.
 
s ravi chandran
Ranch Hand
Posts: 547
5
Java jQuery
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Rahul Mahindrakar wrote:Hi

I changed the code due to the following in the first discussion

now thread1 will check the variable, and if it is 10, this will make it 20. thread2 will check the variable, and if its 10, it will make the variable 30.


Thanks for the refractoring. I am getting more confused with the usecase for volatile now... if it is not safe to use it like this.. then its usage will help in very limited situations...
 
Henry Wong
author
Sheriff
Posts: 22851
119
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
s ravi chandran wrote:
Thanks for the refractoring. I am getting more confused with the usecase for volatile now... if it is not safe to use it like this.. then its usage will help in very limited situations...


Agreed. The volatile keyword by itself have limited usage. It is, however, used internally by the atomic classes, which in turn, is needed to be able to do optimistic locking (or any type of lock-less implementation).

Henry
 
s ravi chandran
Ranch Hand
Posts: 547
5
Java jQuery
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Henry Wong wrote:
s ravi chandran wrote:
Thanks for the refractoring. I am getting more confused with the usecase for volatile now... if it is not safe to use it like this.. then its usage will help in very limited situations...


Agreed. The volatile keyword by itself have limited usage. It is, however, used internally by the atomic classes, which in turn, is needed to be able to do optimistic locking (or any type of lock-less implementation).

Henry


Okay, got the concept now.. Thanks everyone!!
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!