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

  • Home
  • SEARCH
  • 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 8620863
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 12, 20262026-06-12T06:41:01+00:00 2026-06-12T06:41:01+00:00

The below code is a very simplified example of what I am trying to

  • 0

The below code is a very simplified example of what I am trying to accomplish.

 private object _highestLevelLock = new object();
    private int _highestValue;
    private void SetHighestValue()
    {
        var random = new Random();
        var next = random.Next(0, 100);
        if (next > _highestValue)
        {
            lock (_highestLevelLock)
            {
                if(next > _highestValue)
                    _highestValue = next;
            }
        }
    }

Namely, I have a variable that holds the highest integer that I have come across so far. SetHighestValue() can be accessed by multiple threads.

If the random integer generated is larger than the currently largest integer, I will update the _highestValue.

My question is, how can I avoid checking twice if next > highestValue? If I remove it from inside the lock, then there is the risk that _highestValue will get set to something even higher, before this thread sets the value, and in this scenario, _highestValue will be inaccurate.

I know that I can get rid of the if statement outside the lock, but I do not want to lock unnecessarily.

Is there a better way to accomplish this? Potentially some combination of locking and/or using the Interlocked namespace?

On a somewhat related note, should _highestValue be volatile? Even if so, I assume that will not help with my question at hand.

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-12T06:41:02+00:00Added an answer on June 12, 2026 at 6:41 am

    What you are describing is a case of test and test and set and is a very efficient way of handling things. To answer your question, short of encapsulating your condition in a function there is no way to get rid of the code duplication.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

The below code is very simplified. I'm trying to abstract a dispatcher context so
I am very new to asp classic application. The below code snippet is from
In the simplified code below, if(city == New York City) { var MyObject =
Im using code very similar to below: - (void)flipToViewController:(UIViewController*)targetViewController transition:(UIViewAnimationTransition)transition { if( targetViewController )
The below code is very simple. I have a jQuery autocomplete bound to an
I am having a very specific problem in JQuery The code below is used
I have written a very simple WCF service, that worked fine (code below), then
I am writing a very simple HTML code which is listed below. Written in
Below code : URL oracle = new URL(http://www.oracle.com/); URLConnection inputStream =oracle.openConnection(); InputStream in =
I have some very simple code below that I can't get to validate on

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.