Lock vs Volatile

08/16/2011 13:32 BaussHacker#1
What would be best to use in which situation?

If you don't know what Volatile is:
[Only registered and activated users can see links. Click Here To Register...]

Quote:
Volatile keyword is used to define a variable which is to be modified across multiple threads without invoking lock statements(Although we do lock them most of the times). Volatile variables are not subject to compiler optimization and thus we will get the most updated value of the variable all the time. See the example below :
:D
08/16/2011 14:19 Sp!!ke#2
I will vote for volatile cause lock in most cases block...
08/16/2011 15:36 _DreadNought_#3
No idea, was looking but the ?? operator caught my eye.

Heh it's pretty cool actually.
Code:
static GameClient xx = null;
void test()
{
      xx = xx ?? new GameClient(null);
}
08/16/2011 15:51 BaussHacker#4
Quote:
Originally Posted by _DreadNought_ View Post
No idea, was looking but the ?? operator caught my eye.

Heh it's pretty cool actually.
Code:
static GameClient xx = null;
void test()
{
      xx = xx ?? new GameClient(null);
}
You didn't know about nullable types?

Quote:
Originally Posted by Sp!!ke View Post
I will vote for volatile cause lock in most cases block...
Yea, that's what I thought. When you lock something, then it can't be changed, where volatile will just give you the most recent update. Just wanna know which cases I should use what.
08/16/2011 15:55 { Angelius }#5
Quote:
Originally Posted by Sp!!ke View Post
I will vote for volatile cause lock in most cases block...
thats why they have the Monitor.enter() method

Monitor.enter(object)
try
{

}
finally { Monitor.exit(object); }
08/16/2011 16:02 BaussHacker#6
Quote:
Originally Posted by { Angelius } View Post
thats why they have the Monitor.enter() method

Monitor.enter(object)
try
{

}
finally { Monitor.exit(object); }
You shouldn't use Monitor for every thread.
08/16/2011 16:36 { Angelius }#7
Quote:
Originally Posted by BaussHacker View Post
You shouldn't use Monitor for every thread.
if you know how to use it you should be able to use it whenever you need it and with out any problems .

IE.
Monitor.enter(object)
try
{
add or copy or remove or value =
from object and keep going
}
finally { Monitor.exit(object); }

none of whats mentioned up there blocks .

any exceptions and the lock well be released giving the next waiting thread a chance to obtain the lock . a catch(Ex) might be needed

@BaussHacker the attached is something that can be useful. why not create/edit your own monitor. its meant to deal with any dead locks (if any located)

another PS. i dident create that monitor class in side attached .cs file
08/16/2011 17:10 BaussHacker#8
Quote:
Originally Posted by { Angelius } View Post
if you know how to use it you should be able to use it whenever you need it and with out any problems .

IE.
Monitor.enter(object)
try
{
add or copy or remove or value =
from object and keep going
}
finally { Monitor.exit(object); }

none of whats mentioned up there blocks .

any exceptions and the lock well be released giving the next waiting thread a chance to obtain the lock . a catch(Ex) might be needed

@BaussHacker the attached is something that can be useful. why not create/edit your own monitor. its meant to deal with any dead locks (if any located)

another PS. i dident create that monitor class in side attached .cs file
Well my problem is not lock, just want to know, when I should use what ^^
08/16/2011 17:37 Korvacs#9
Lock just wraps monitor btw, so you can use Monitor wherever you like, just know that lock uses monitor and a try/catch to ensure that you can release the lock if a thread crashes. Monitor doesnt release the lock if a thread crashes in the middle of your code before you manually release it.

Lock should be used if you need to both read and write to something on seperate threads to ensure that it remains threadsafe.

So Volatile could be use if your only going to be reading or only writing to an object.
08/16/2011 17:46 InfamousNoone#10
Volatile should be if you're only reading from the variable imo
08/16/2011 19:06 BaussHacker#11
Alright thank you Korvacs and Hybrid :)