How to implement a data class that will be accessed/updated by multiple threads? [on hold]












-2












$begingroup$


I am trying to implement a data class called GamePlayData that stores game play counts. It has two members one called lifetime, which stores the game play count since the machine was configured for the first time. The other member is called period, which stores play count for the configured period of time.



I have come up with the following class:



public class GamePlayData
{
private KeyValueDatabase _database;
private long? _period;
private long? _lifetime;

private readonly string _lifetimeKey;
private readonly string _periodKey;

private readonly object _exclusion = new object();

public GamePlayData(KeyValueDatabase database)
{
_database = database;
_lifetimeKey = @"GamePlayDataLifetime"
_periodKey = @"GamePlayDataPeriod"
}

public long Period
{
get
{
lock (_exclusion)
{
if (_period != null)
{
return (long)_period;
}

_database.GetValue(_periodKey, out long value);
_period = value;
return (long)_period;
}
}
}

public long Lifetime
{
get
{
lock (_exclusion)
{
if (_lifetime != null)
{
return (long)_lifetime;
}

_database.GetValue(_lifetimeKey, out long value);
_lifetime = value;
return (long)_lifetime;
}
}
}

public void Increment(long amount)
{
lock (_exclusion)
{
if (_lifetime == null)
{
_database.GetValue(_lifetimeKey, out long value);
_lifetime = value;
}

if (_period == null)
{
_database.GetValue(_periodKey, out long value);
_period = value;
}

if (_lifetime + amount <= 0)
{
_lifetime = 0;
}
else
{
_lifetime = (_lifetime + amount);
}

if (_period + amount <= 0)
{
_period = 0;
}
else
{
_period = (_period + amount);
}

_database.SetValue(_lifetimeKey, _lifetime);
_database.SetValue(_periodKey, _period);

} // lock
}

public void Reset(long resetValue)
{
lock (_exclusion)
{
_lifetime = resetValue;
_period = resetValue;
}
}
}


The problem is that using locks here could potentially degrade the performance as contention locks are kinda slow.



I am wondering if there are some improvements I could do here or may be implement this class altogether in a different way to get more performance.



Any ideas ?










share|improve this question









New contributor




Monku is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.







$endgroup$



put on hold as off-topic by πάντα ῥεῖ, t3chb0t, VisualMelon, Mast, yuri 10 mins ago


This question appears to be off-topic. The users who voted to close gave these specific reasons:



  • "Code not implemented or not working as intended: Code Review is a community where programmers peer-review your working code to address issues such as security, maintainability, performance, and scalability. We require that the code be working correctly, to the best of the author's knowledge, before proceeding with a review." – Mast, yuri

  • "Lacks concrete context: Code Review requires concrete code from a project, with sufficient context for reviewers to understand how that code is used. Pseudocode, stub code, hypothetical code, obfuscated code, and generic best practices are outside the scope of this site." – πάντα ῥεῖ, t3chb0t, VisualMelon


If this question can be reworded to fit the rules in the help center, please edit the question.












  • 1




    $begingroup$
    Why do you think you need locking? Please tell us more about your game and how this class is used.
    $endgroup$
    – t3chb0t
    11 hours ago










  • $begingroup$
    I need locking to solve contention caused by different threads trying to read/write to this data.
    $endgroup$
    – Monku
    11 hours ago
















-2












$begingroup$


I am trying to implement a data class called GamePlayData that stores game play counts. It has two members one called lifetime, which stores the game play count since the machine was configured for the first time. The other member is called period, which stores play count for the configured period of time.



I have come up with the following class:



public class GamePlayData
{
private KeyValueDatabase _database;
private long? _period;
private long? _lifetime;

private readonly string _lifetimeKey;
private readonly string _periodKey;

private readonly object _exclusion = new object();

public GamePlayData(KeyValueDatabase database)
{
_database = database;
_lifetimeKey = @"GamePlayDataLifetime"
_periodKey = @"GamePlayDataPeriod"
}

public long Period
{
get
{
lock (_exclusion)
{
if (_period != null)
{
return (long)_period;
}

_database.GetValue(_periodKey, out long value);
_period = value;
return (long)_period;
}
}
}

public long Lifetime
{
get
{
lock (_exclusion)
{
if (_lifetime != null)
{
return (long)_lifetime;
}

_database.GetValue(_lifetimeKey, out long value);
_lifetime = value;
return (long)_lifetime;
}
}
}

public void Increment(long amount)
{
lock (_exclusion)
{
if (_lifetime == null)
{
_database.GetValue(_lifetimeKey, out long value);
_lifetime = value;
}

if (_period == null)
{
_database.GetValue(_periodKey, out long value);
_period = value;
}

if (_lifetime + amount <= 0)
{
_lifetime = 0;
}
else
{
_lifetime = (_lifetime + amount);
}

if (_period + amount <= 0)
{
_period = 0;
}
else
{
_period = (_period + amount);
}

_database.SetValue(_lifetimeKey, _lifetime);
_database.SetValue(_periodKey, _period);

} // lock
}

public void Reset(long resetValue)
{
lock (_exclusion)
{
_lifetime = resetValue;
_period = resetValue;
}
}
}


The problem is that using locks here could potentially degrade the performance as contention locks are kinda slow.



I am wondering if there are some improvements I could do here or may be implement this class altogether in a different way to get more performance.



Any ideas ?










share|improve this question









New contributor




Monku is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.







$endgroup$



put on hold as off-topic by πάντα ῥεῖ, t3chb0t, VisualMelon, Mast, yuri 10 mins ago


This question appears to be off-topic. The users who voted to close gave these specific reasons:



  • "Code not implemented or not working as intended: Code Review is a community where programmers peer-review your working code to address issues such as security, maintainability, performance, and scalability. We require that the code be working correctly, to the best of the author's knowledge, before proceeding with a review." – Mast, yuri

  • "Lacks concrete context: Code Review requires concrete code from a project, with sufficient context for reviewers to understand how that code is used. Pseudocode, stub code, hypothetical code, obfuscated code, and generic best practices are outside the scope of this site." – πάντα ῥεῖ, t3chb0t, VisualMelon


If this question can be reworded to fit the rules in the help center, please edit the question.












  • 1




    $begingroup$
    Why do you think you need locking? Please tell us more about your game and how this class is used.
    $endgroup$
    – t3chb0t
    11 hours ago










  • $begingroup$
    I need locking to solve contention caused by different threads trying to read/write to this data.
    $endgroup$
    – Monku
    11 hours ago














-2












-2








-2





$begingroup$


I am trying to implement a data class called GamePlayData that stores game play counts. It has two members one called lifetime, which stores the game play count since the machine was configured for the first time. The other member is called period, which stores play count for the configured period of time.



I have come up with the following class:



public class GamePlayData
{
private KeyValueDatabase _database;
private long? _period;
private long? _lifetime;

private readonly string _lifetimeKey;
private readonly string _periodKey;

private readonly object _exclusion = new object();

public GamePlayData(KeyValueDatabase database)
{
_database = database;
_lifetimeKey = @"GamePlayDataLifetime"
_periodKey = @"GamePlayDataPeriod"
}

public long Period
{
get
{
lock (_exclusion)
{
if (_period != null)
{
return (long)_period;
}

_database.GetValue(_periodKey, out long value);
_period = value;
return (long)_period;
}
}
}

public long Lifetime
{
get
{
lock (_exclusion)
{
if (_lifetime != null)
{
return (long)_lifetime;
}

_database.GetValue(_lifetimeKey, out long value);
_lifetime = value;
return (long)_lifetime;
}
}
}

public void Increment(long amount)
{
lock (_exclusion)
{
if (_lifetime == null)
{
_database.GetValue(_lifetimeKey, out long value);
_lifetime = value;
}

if (_period == null)
{
_database.GetValue(_periodKey, out long value);
_period = value;
}

if (_lifetime + amount <= 0)
{
_lifetime = 0;
}
else
{
_lifetime = (_lifetime + amount);
}

if (_period + amount <= 0)
{
_period = 0;
}
else
{
_period = (_period + amount);
}

_database.SetValue(_lifetimeKey, _lifetime);
_database.SetValue(_periodKey, _period);

} // lock
}

public void Reset(long resetValue)
{
lock (_exclusion)
{
_lifetime = resetValue;
_period = resetValue;
}
}
}


The problem is that using locks here could potentially degrade the performance as contention locks are kinda slow.



I am wondering if there are some improvements I could do here or may be implement this class altogether in a different way to get more performance.



Any ideas ?










share|improve this question









New contributor




Monku is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.







$endgroup$




I am trying to implement a data class called GamePlayData that stores game play counts. It has two members one called lifetime, which stores the game play count since the machine was configured for the first time. The other member is called period, which stores play count for the configured period of time.



I have come up with the following class:



public class GamePlayData
{
private KeyValueDatabase _database;
private long? _period;
private long? _lifetime;

private readonly string _lifetimeKey;
private readonly string _periodKey;

private readonly object _exclusion = new object();

public GamePlayData(KeyValueDatabase database)
{
_database = database;
_lifetimeKey = @"GamePlayDataLifetime"
_periodKey = @"GamePlayDataPeriod"
}

public long Period
{
get
{
lock (_exclusion)
{
if (_period != null)
{
return (long)_period;
}

_database.GetValue(_periodKey, out long value);
_period = value;
return (long)_period;
}
}
}

public long Lifetime
{
get
{
lock (_exclusion)
{
if (_lifetime != null)
{
return (long)_lifetime;
}

_database.GetValue(_lifetimeKey, out long value);
_lifetime = value;
return (long)_lifetime;
}
}
}

public void Increment(long amount)
{
lock (_exclusion)
{
if (_lifetime == null)
{
_database.GetValue(_lifetimeKey, out long value);
_lifetime = value;
}

if (_period == null)
{
_database.GetValue(_periodKey, out long value);
_period = value;
}

if (_lifetime + amount <= 0)
{
_lifetime = 0;
}
else
{
_lifetime = (_lifetime + amount);
}

if (_period + amount <= 0)
{
_period = 0;
}
else
{
_period = (_period + amount);
}

_database.SetValue(_lifetimeKey, _lifetime);
_database.SetValue(_periodKey, _period);

} // lock
}

public void Reset(long resetValue)
{
lock (_exclusion)
{
_lifetime = resetValue;
_period = resetValue;
}
}
}


The problem is that using locks here could potentially degrade the performance as contention locks are kinda slow.



I am wondering if there are some improvements I could do here or may be implement this class altogether in a different way to get more performance.



Any ideas ?







c#






share|improve this question









New contributor




Monku is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.











share|improve this question









New contributor




Monku is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.









share|improve this question




share|improve this question








edited 10 hours ago







Monku













New contributor




Monku is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.









asked 11 hours ago









MonkuMonku

1002




1002




New contributor




Monku is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.





New contributor





Monku is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.






Monku is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.




put on hold as off-topic by πάντα ῥεῖ, t3chb0t, VisualMelon, Mast, yuri 10 mins ago


This question appears to be off-topic. The users who voted to close gave these specific reasons:



  • "Code not implemented or not working as intended: Code Review is a community where programmers peer-review your working code to address issues such as security, maintainability, performance, and scalability. We require that the code be working correctly, to the best of the author's knowledge, before proceeding with a review." – Mast, yuri

  • "Lacks concrete context: Code Review requires concrete code from a project, with sufficient context for reviewers to understand how that code is used. Pseudocode, stub code, hypothetical code, obfuscated code, and generic best practices are outside the scope of this site." – πάντα ῥεῖ, t3chb0t, VisualMelon


If this question can be reworded to fit the rules in the help center, please edit the question.







put on hold as off-topic by πάντα ῥεῖ, t3chb0t, VisualMelon, Mast, yuri 10 mins ago


This question appears to be off-topic. The users who voted to close gave these specific reasons:



  • "Code not implemented or not working as intended: Code Review is a community where programmers peer-review your working code to address issues such as security, maintainability, performance, and scalability. We require that the code be working correctly, to the best of the author's knowledge, before proceeding with a review." – Mast, yuri

  • "Lacks concrete context: Code Review requires concrete code from a project, with sufficient context for reviewers to understand how that code is used. Pseudocode, stub code, hypothetical code, obfuscated code, and generic best practices are outside the scope of this site." – πάντα ῥεῖ, t3chb0t, VisualMelon


If this question can be reworded to fit the rules in the help center, please edit the question.








  • 1




    $begingroup$
    Why do you think you need locking? Please tell us more about your game and how this class is used.
    $endgroup$
    – t3chb0t
    11 hours ago










  • $begingroup$
    I need locking to solve contention caused by different threads trying to read/write to this data.
    $endgroup$
    – Monku
    11 hours ago














  • 1




    $begingroup$
    Why do you think you need locking? Please tell us more about your game and how this class is used.
    $endgroup$
    – t3chb0t
    11 hours ago










  • $begingroup$
    I need locking to solve contention caused by different threads trying to read/write to this data.
    $endgroup$
    – Monku
    11 hours ago








1




1




$begingroup$
Why do you think you need locking? Please tell us more about your game and how this class is used.
$endgroup$
– t3chb0t
11 hours ago




$begingroup$
Why do you think you need locking? Please tell us more about your game and how this class is used.
$endgroup$
– t3chb0t
11 hours ago












$begingroup$
I need locking to solve contention caused by different threads trying to read/write to this data.
$endgroup$
– Monku
11 hours ago




$begingroup$
I need locking to solve contention caused by different threads trying to read/write to this data.
$endgroup$
– Monku
11 hours ago










0






active

oldest

votes

















0






active

oldest

votes








0






active

oldest

votes









active

oldest

votes






active

oldest

votes

Popular posts from this blog

How to reconfigure Docker Trusted Registry 2.x.x to use CEPH FS mount instead of NFS and other traditional...

is 'sed' thread safe

How to make a Squid Proxy server?