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

标签 c# multithreading

我有以下程序:

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);
    }
}

如何编译?

最佳答案

基本上,你不能。该框架不提供对任意结构执行原子、无锁操作的方法。

关于C# 如何比较和交换两个结构?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35160448/

相关文章:

java - 使用 synchronizedSet 同步两个线程之间的访问

Java:这种方法线程安全吗?

c# - 如何启动一个线程来保持GUI刷新?

c# - 在 SQL Server 中存储非常大的数字

c# - 重复调用HashCode.Combine

c# - 使用任务并行库时与对象同步

c# - 从 Azure 函数将批量消息发送到服务总线主题

C# - 仅使用线程运行立即函数?

c# - 枚举到下拉框到方法

python - 如何在 Python 中分析多线程程序的内存?