I read about lock and [MethodImpl(MethodImplOptions.Synchronized)]....both prevents multiple threads from accessing a code concurrently.
Is this ensure the block get executed without thread switching...the thread from getting switched in middle of executing the block.
Expand|Select|Wrap|Line Numbers
- Class ClassCalc
- {
- // implementation
- }
- class Update
- {
- ClassCalc calc;
- public void UpdateValue()
- {
- if(calc.GetValue() > 0) // check whether the value of some
- //variable in calc is > 0
- {
- //Do something
- calc.SetValue(somevalue); // Set the variable.
- }
- }
- }
- The SetValue and GetValue being public can be accessed by some other application.
- How can i ensure that no other thread update the variable in between GetValue() and SetValue() in the above code.