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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 13, 20262026-05-13T16:11:46+00:00 2026-05-13T16:11:46+00:00

Disclaimer: My posts are apparently always verbose. If you happen to know the answer

  • 0

Disclaimer: My posts are apparently always verbose. If you happen to know the answer to the title question, feel free to just answer it without reading my extended discussion below.


The System.Threading.Interlocked class provides some very useful methods to assist in writing thread-safe code. One of the more complex methods is CompareExchange, which can be used for computing a running total that may be updated from multiple threads.

Since the use of CompareExchange is a bit tricky, I thought it a rather common-sense idea to provide some helper methods for it:

// code mangled so as not to require horizontal scrolling
// (on my monitor, anyway)
public static double Aggregate
(ref double value, Func<double, double> aggregator) {
    double initial, aggregated;

    do {
        initial = value;
        aggregated = aggregator(initial);
    } while (
        initial != Interlocked.CompareExchange(ref value, aggregated, initial)
    );

    return aggregated;
}

public static double Increase(ref double value, double amount) {
    return Aggregate(ref value, delegate(double d) { return d + amount; });
}

public static double Decrease(ref double value, double amount) {
    return Aggregate(ref value, delegate(double d) { return d - amount; });
}

Now, perhaps I am just guilty of being generic-happy (I will admit, this is often true); but it does feel silly to me to restrict the functionality provided by the above methods to double values only (or, more accurately, for me to have to write overloaded versions of the above methods for every type I want to support). Why can’t I do this?

// the code mangling continues...
public static T Aggregate<T>
(ref T value, Func<T, T> aggregator) where T : IEquatable<T> {
    T initial, aggregated;

    do {
        initial = value;
        aggregated = aggregator(initial);
    } while (
        !initial.Equals(
            Interlocked.CompareExchange<T>(ref value, aggregated, initial)
        )
    );
}

I can’t do this because Interlocked.CompareExchange<T> apparently has a where T : class constraint, and I don’t understand why. I mean, maybe it’s because there are already overloads for CompareExchange that accept Int32, Int64, Double, etc.; but that hardly seems a good rationale. In my case, for example, it would be quite handy to be able to use the Aggregate<T> method to perform a wide range of atomic calculations.

  • 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-13T16:11:46+00:00Added an answer on May 13, 2026 at 4:11 pm

    Interlocked.CompareExchange is meant to be implemented with native atomic instructions provided directly by the processor. It’s pointless to have something like that use a lock internally (it’s designed for lock-free scenarios).

    Processors that provide atomic compare exchange instruction naturally support it as small, “register-sized” operations (e.g. the largest compare-exchange instruction on an Intel x64 processor is cmpxchg16b that works on 128 bit values).

    An arbitrary value type can be potentially bigger than that and compare-exchanging it may not be possible with a single instruction. Compare-exchanging a reference type is easy. Regardless of its total size in memory, you’ll be comparing and copying a small pointer of a known size. This is also true for primitive types like Int32 and Double—all of them are small.

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

Sidebar

Related Questions

DISCLAIMER: This is not a real-world example. It is just a theoretical question of
DISCLAIMER: I did search exhaustively for an answer to this question, and yes, I
Disclaimer: I'm a new convert to iOS, so please feel free to tell me
Disclaimer: I know there are better ways to track email open rates, which are
Disclaimer: this question is driven by my personal curiosity more than an actual need
(DISCLAIMER: I'm not a programmer, I spend my time on serverfault, I'm just a
Disclaimer: please do not bother reading this long post unless you are an embedded
[DISCLAIMER: My development machine is running OS X Tiger, so my question and experiences
(Disclaimer) MY php experience is approx 2 hours old and I have know idea
Disclaimer: I'm not a Notes admin, I just wrote the application :), and 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.