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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 23, 20262026-05-23T18:13:02+00:00 2026-05-23T18:13:02+00:00

System.Threading.Interlocked.CompareExchange operator provides atomic (thus thread-safe) C# implementation of the Compare-And-Swap operation. For example

  • 0

System.Threading.Interlocked.CompareExchange operator provides atomic (thus thread-safe) C# implementation of the Compare-And-Swap operation.

For example int i = 5; Interlocked.CompareExchange(ref i, 10, 5); After this command, the int i would have a value = 10. And also the compare and exchange happens atomically (single operation).

When I tried using this with a class instance, the compare fails and the values are not exchanged.

   public class X
   {
       public int y;
       public X(int val) { y = val; }
   }

Now when I do

    X a = new X(1);
    X b = new X(1);
    X c = new X(2);
    Interlocked.CompareExchange<X>(ref a, c, b);

The compare and Exchange operation fails. So, I overrided the Equals and the == operator for the class X as

    public override bool Equals(object obj) { return y == ((X) obj).y; }

So, now I get Interlocked.Equals(a,b) as true, but the CompareExchange operations still fails.

Is there any method to do this? I want to compare two class instances and assign one of them a value based on the comparision.

  • 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-23T18:13:03+00:00Added an answer on May 23, 2026 at 6:13 pm

    No. It can’t be done.

    Interlocked.CompareExchange basically maps directly to an assembly instruction which is able to atomically compare and swap the contents of a memory address. I believe in 32-bit mode, a 64-bit version of the instruction is available (as well as 32- and 16-bit versions), and in 64-bit mode, I think a 128-bit version is available. But that’s all. The CPU doesnt’ have a “swap .NET class based on its specific Equals function” instruction.

    If you want to swap arbitrary objects, using arbitrary equality functions, you have to do it yourself, using locks or other synchronization mechanisms.

    There is an overload of the Interlocked.CompareExchange function which works on object references, but it uses reference equality for the above reason. It simply compares the references, and then swaps them.

    In response to your comment, using structs would not solve the problem. Again, the CPU can only atomically compare and swap values of certain fixed sizes, and it has no notion of abstract datatypes. Reference types can be used because the reference itself has a valid size, and can be compared against another reference by the CPU. But the CPU knows nothing about the object that the reference points to.

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

Sidebar

Related Questions

The following function System.Threading.Thread.Sleep(); delay the thread in millisecond, and take the integer value
From time to time I get a System.Threading.ThreadStateException when attempting to restart a thread.
I'm trying to create a new System.Threading.Thread object using Jscript, but I can't get
string strdate=15/06/2010; DateTime dt = DateTime.Parse(strdate, System.Threading.Thread.CurrentThread.CurrentCulture.DateTimeFormat); i cannot able to get the datetime
System.Runtime.Serialization.SerializationException: Type 'System.Threading.Thread' in Assembly 'mscorlib, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089' is not marked as serializable.
I noticed that System.Threading.Thread implements a finalizer but not IDisposable. The recommended practice is
Example System.Threading.AutoResetEvent e = new System.Threading.AutoResetEvent(false); bool b = e.WaitOne(1000, false); I've done a
I got a System.Threading.Timer which I change to lower priority ( Thread.CurrentThread.Priority = XXXX
what happens if i put a System.Threading.Thread.Sleep() into a Task that is started with
I would like to know how System.Threading.Thread works in the CLR. I guess threads

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.