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 8999117
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 16, 20262026-06-16T00:08:15+00:00 2026-06-16T00:08:15+00:00

Let’s say I have two methods – getCurrentValue(int valueID) updateValues(int changedComponentID) These two methods

  • 0

Let’s say I have two methods –

getCurrentValue(int valueID)
updateValues(int changedComponentID)

These two methods are called on the same object independently by separate threads.

getCurrentValue() simply does a database look-up for the current valueID.

“Values” change if their corresponding components change. The updateValues()method updates those values that are dependent upon the component that just changed, i.e. changedComponentID. This is a database operation and takes time.

While this update operation is going on, I do not want to return a stale value by doing a lookup from the database, but I want to wait till the update method has completed. At the same time, I don’t want two update operations to happen simultaneously or an update to happen when a read is going on.

So, I’m thinking of doing it this way –

[MethodImpl(MethodImplOptions.Synchronized)]
public int getCurrentValue(int valueID)
     {
        while(updateOperationIsGoingOn)
          {
             // do nothing  
          }
        readOperationIsGoingOn = true;

        value = // read value from DB

        readOperationIsGoingOn = false; 
        return value;
     }

[MethodImpl(MethodImplOptions.Synchronized)]
public void updateValues(int componentID)
     {
         while(readOperationIsGoingOn)
          {
             // do nothing
          }
         updateOperationIsGoingOn = true;

         // update values in DB

         updateOperationIsGoingOn = false;
     }

I’m not sure whether this is a correct way of doing it. Any suggestions? Thanks.

  • 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-06-16T00:08:16+00:00Added an answer on June 16, 2026 at 12:08 am

    That’s not the correct way. Like this you are doing an “active wait”, effectively blocking your CPU.

    You should use a lock instead:

    static object _syncRoot = new object();
    public int getCurrentValue(int valueID)
    {
        lock(_syncRoot)
        {
            value = // read value from DB
        }
    }
    
    public void updateValues(int componentID)
    {
        lock(_syncRoot)
        {
            // update values in DB
        }
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Let's say I have two text files that I need to extract data out
Let's say we have two pages in Orchard CMS. The homepage and the About
Let's say I have two Entity classes: SocialApp and SocialAppType In SocialApp I have
Let's say I have an abstract parent class called shape, and that there are
Let's say I have many items with click event (all have same element name
Let's say that I have a rails plugin called wipy on GitHub. It has
Let's say that I have classes like this: public class Parent { public int
Let's say I have 2 functions: void function1(int *ptr) { printf(%d, *ptr); } and
Let's say I have two objects, Master and Slave . Slave has a method
Let's say I have the following two lists of tuples myList = [(1, 7),

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.