Register for your free account! | Forgot your password?

You last visited: Today at 13:47

  • Please register to post and access all features, it's quick, easy and FREE!

Advertisement



Lock vs Volatile

Discussion on Lock vs Volatile within the CO2 Private Server forum part of the Conquer Online 2 category.

Reply
 
Old   #1
 
elite*gold: 0
Join Date: May 2011
Posts: 1,769
Received Thanks: 756
Lock vs Volatile

What would be best to use in which situation?

If you don't know what Volatile is:


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 :
BaussHacker is offline  
Old 08/16/2011, 14:19   #2
 
Sp!!ke's Avatar
 
elite*gold: 0
Join Date: Nov 2009
Posts: 380
Received Thanks: 58
I will vote for volatile cause lock in most cases block...
Sp!!ke is offline  
Old 08/16/2011, 15:36   #3
 
_DreadNought_'s Avatar
 
elite*gold: 28
Join Date: Jun 2010
Posts: 2,223
Received Thanks: 867
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);
}
_DreadNought_ is offline  
Old 08/16/2011, 15:51   #4
 
elite*gold: 0
Join Date: May 2011
Posts: 1,769
Received Thanks: 756
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.
BaussHacker is offline  
Old 08/16/2011, 15:55   #5
 
{ Angelius }'s Avatar
 
elite*gold: 0
Join Date: Aug 2010
Posts: 991
Received Thanks: 1,107
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); }
{ Angelius } is offline  
Old 08/16/2011, 16:02   #6
 
elite*gold: 0
Join Date: May 2011
Posts: 1,769
Received Thanks: 756
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.
BaussHacker is offline  
Old 08/16/2011, 16:36   #7
 
{ Angelius }'s Avatar
 
elite*gold: 0
Join Date: Aug 2010
Posts: 991
Received Thanks: 1,107
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
{ Angelius } is offline  
Old 08/16/2011, 17:10   #8
 
elite*gold: 0
Join Date: May 2011
Posts: 1,769
Received Thanks: 756
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 ^^
BaussHacker is offline  
Old 08/16/2011, 17:37   #9


 
Korvacs's Avatar
 
elite*gold: 20
Join Date: Mar 2006
Posts: 6,125
Received Thanks: 2,518
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.
Korvacs is offline  
Thanks
1 User
Old 08/16/2011, 17:46   #10
 
InfamousNoone's Avatar
 
elite*gold: 20
Join Date: Jan 2008
Posts: 2,012
Received Thanks: 2,882
Volatile should be if you're only reading from the variable imo
InfamousNoone is offline  
Thanks
1 User
Old 08/16/2011, 19:06   #11
 
elite*gold: 0
Join Date: May 2011
Posts: 1,769
Received Thanks: 756
Alright thank you Korvacs and Hybrid
BaussHacker is offline  
Reply


Similar Threads Similar Threads
No lock
04/23/2011 - DarkOrbit - 7 Replies
Hi guys i looking for No lock bot ... do u have some 1 that clever toy ? please :.. ty
Lock mob
03/15/2010 - Nostale - 1 Replies
Sie können eine lockmob wie metin2?
Betteln an die lock mob götter S 15 lock mob
02/04/2009 - Metin2 - 1 Replies
dieser thread geht vor allen dingen an banjo aber wers sonst noch kann kann den lock mob ja auch mal updaten also pls lock mob für s15/16
Mob Lock
10/02/2008 - Metin2 Private Server - 4 Replies
Hi habe gerade auf dem Pserver angefangen und wollte fragen ob es auch dafüreinen Mob Lock gibt



All times are GMT +2. The time now is 13:47.


Powered by vBulletin®
Copyright ©2000 - 2024, Jelsoft Enterprises Ltd.
SEO by vBSEO ©2011, Crawlability, Inc.
This site is protected by reCAPTCHA and the Google Privacy Policy and Terms of Service apply.

Support | Contact Us | FAQ | Advertising | Privacy Policy | Terms of Service | Abuse
Copyright ©2024 elitepvpers All Rights Reserved.