简体   繁体   English

C#如何比较和交换两个结构?

[英]C# How do I compare and exchange two structs?

I have the following program: 我有以下程序:

public class Program
{
    struct Foo
    {
        public int Bar;
        public int Zoo;
    }

    public static void Main()
    {
        Foo a;
        Foo b;

        a.Bar = 5;
        a.Zoo = 2;
        b.Bar = 5;
        a.Zoo = 2;

        Foo c;
        c.Bar = 3;
        c.Zoo = 5;

        var result = Interlocked.CompareExchange(ref a, b, c);
    }
}

How can I make this compile? 我该如何进行编译?

You can't, basically. 基本上你不能。 The framework doesn't provide a way of performing atomic, lock-free operations on arbitrary structs. 该框架没有提供对任意结构执行原子无锁操作的方法。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM