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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 14, 20262026-06-14T11:00:46+00:00 2026-06-14T11:00:46+00:00

The System.Threading.Interlocked object allows for Addition (subtraction) and comparison as an atomic operation. It

  • 0

The System.Threading.Interlocked object allows for Addition (subtraction) and comparison as an atomic operation. It seems that a CompareExchange that just doesn’t do equality but also GreaterThan/LessThan as an atomic comparison would be quite valuable.

Would a hypothetical Interlocked.GreaterThan a feature of the IL or is it a CPU-level feature? Both?

Lacking any other option, is it possible to create such a feature in C++ or direct IL code and expose that functionality to C#?

  • 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-14T11:00:48+00:00Added an answer on June 14, 2026 at 11:00 am

    Update to the later post I made here: we found a better way to make the greater comparison by using additional lock object. We wrote many unit tests in order to validate that a lock and Interlocked can be used together, but only for some cases.

    How the code works: Interlocked uses memory barriers that a read or write is atomic. The sync-lock is needed to make the greater-than comparison an atomic operation. So the rule now is that inside this class no other operation writes the value without this sync lock.

    What we get with this class is an interlocked value which can be read very fast, but write takes a little bit more. Read is about 2-4 times faster in our application.

    Here the code as view:

    See here: http://files.thekieners.com/blogcontent/2012/ExchangeIfGreaterThan2.png

    Here as code to copy&paste:

    public sealed class InterlockedValue
    {
        private long _myValue;
        private readonly object _syncObj = new object();
    
        public long ReadValue()
        {
            // reading of value (99.9% case in app) will not use lock-object, 
            // since this is too much overhead in our highly multithreaded app.
            return Interlocked.Read(ref _myValue);
        }
    
        public bool SetValueIfGreaterThan(long value)
        {
            // sync Exchange access to _myValue, since a secure greater-than comparisons is needed
            lock (_syncObj)
            {
                // greather than condition
                if (value > Interlocked.Read(ref  _myValue))
                {
                    // now we can set value savely to _myValue.
                    Interlocked.Exchange(ref _myValue, value);
                    return true;
                }
                return false;
            }
        }
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

System.Threading.Interlocked.CompareExchange operator provides atomic (thus thread-safe) C# implementation of the Compare-And-Swap operation. For example
I'm trying to reproduce the following IL using Mono.Cecil: call !!0 [mscorlib]System.Threading.Interlocked::CompareExchange<class [System]System.ComponentModel.PropertyChangedEventHandler>(!!0&, !!0,
I'm trying to create a new System.Threading.Thread object using Jscript, but I can't get
I'm developing an app that starts a System.Threading.Timer which does some fairly rapid reading/writing
I have a .NET System.Threading.Timer timer that ticks every 60 seconds and introduces a
When I use Parallel.ForEach or Parallel.For in the System.Threading.Tasks namespace, can I assume that
System.Threading.WaitHandle (and hence ManualResetEvent etc) implement IDisposable explicitly. This seems to discourage it's use
The System.Threading.ConcurrentQueue.TryDequeue method threw an exception the other day that took me totally by
With System.Threading.Tasks.Task<TResult> , I have to manage the exceptions that could be thrown. I'm
I have a System.Threading.Timer that calls its appropriate event handler (callback) every 10 ms

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.