What does the volatile modifier do

A) The value of the object is written immediately on assignment
B) The value is lost after the system reads it.
C) The system stored the value until the end of all current threads
D) The system always reads the current value of a volatile object at the point it is requested

Showing Answers 1 - 5 of 5 Answers

asaf

  • Jan 7th, 2006
 

give a hint to the compiler that the value may change in unpredictable way (by threads)

  Was this answer useful?  Yes

naveentej

  • Jan 7th, 2006
 

Ans: A, D

The system always reads the current value of a volatile object at the point it is requested, even if the previous instruction asked for a value from the same object. Also, the value of the object is written immediately on assignment.

The volatile modifier is usually used for a field that is accessed by multiple threads without using the lock statement to serialize access. Using the volatile modifier ensures that one thread retrieves the most up-to-date value written by another thread.

  Was this answer useful?  Yes

Ans -  A and D

Whenever a volatile is requested, the system returns the current value at the time of the request.
All assignments are written to the object immediately.

Common usage of the volatile modifier is when a particular field is accessed by many threads without using the lock statement to serialize access. So in essence the volatile modifier guarantees that a thread will retrieve the most recent value written by another thread (even if it was modified by the previous instruction from you call).

You are not allowed to use volatile on just any time. The following is a list of types you can implement this modifier on:

  • Any reference type.
  • Any pointer type in a unsafe context
  • sbyte, byte, short, ushort, int, uint, char, float, bool.
  • An enum type with an enum base type of the following: byte, sbyte, short, ushort, int, uint.

  Was this answer useful?  Yes

Give your answer:

If you think the above answer is not correct, Please select a reason and add your answer below.

 

Related Answered Questions

 

Related Open Questions