简体   繁体   中英

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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