Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 4054940
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 20, 20262026-05-20T14:38:20+00:00 2026-05-20T14:38:20+00:00

I have a simple class like : public class XXX { double val1; double

  • 0

I have a simple class like :

  public class XXX
  {
    double val1;
    double val2;
    double delta;
    public void SetValues(double v1, double v2)
    {
      val1 = v1;
      val2 = v2;
      delta = val1 - val2;
    }

    public double Val1 { get { return val1; } }
    public double Val2 { get { return val2; } }
    public double Delta { get { return delta; } }
  }

, one thread for setting values and multiple threads that read the values. So one can use lock() to make all read and writes uninterrupted.
But I am aware that synchronization is never achived, there is always a chance that Val1 - Val2 may not be equal to Delta which I do not care so much. My concern is more about obtaining the stable values via getters. However lock() is expensive for this situation because mostly the readers will work.

The next best thing that come to my mind is using Interlocked.Exchange()

    public void SetValues(double v1, double v2)
    {
      Interlocked.Exchange(ref val1, v1);
      Interlocked.Exchange(ref val2, v2);
      Interlocked.Exchange(ref delta, v1 - v2);
    }

    public double Val1 { 
      get 
      {
        double val = 0;
        Interlocked.Exchange(ref val, val1);
        return val; 
      } 
    }

But the code seems pretty dum to me. I don’t know.

So does lock() makes sense? Should I use the Interlocked.Exchange() for performance gain? Or what else can I do?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-20T14:38:21+00:00Added an answer on May 20, 2026 at 2:38 pm

    For multiple readers / writers scenarios I would use ReaderWriterLockSlim.

    Represents a lock that is used to manage access to a resource, 
    allowing multiple threads for reading or exclusive access for writing.
    
    Use ReaderWriterLockSlim to protect a resource that is read by multiple 
    threads and written to by one thread at a time. ReaderWriterLockSlim 
    allows multiple threads to be in read mode, allows one thread to be in 
    write mode with exclusive ownership of the lock, and allows one thread 
    that has read access to be in upgradeable read mode, from which the 
    thread can upgrade to write mode without having to relinquish its 
    read access to the resource.
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a simple class like this: class A { public: void f(const int&
Say I have a simple address class like below: public class Address { public
I have a simple model like this one: class Artist(models.Model): surname = models.CharField(max_length=200) name
I have a simple ViewModel class like public class TestViewModel { public String Information
I have a simple class like this [ActiveRecord(Subscriptions)] public class Subscription : ActiveRecordBase<Subscription> {
I have a simple class that looks like this: public class Item { //
If I have a simple class like this: class MyClass { MyClass(){} ~MyClass(){} public:
If I have a simple class like this one for a card: class Card
Suppose I have a simple Java class like this: public class User { String
so i have a class like: class mySafeData { public: void Set(int i) {

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.