哪些操作是原子在C#中? [英] What operations are atomic in C#?

查看:367
本文介绍了哪些操作是原子在C#中?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

有没有一种系统的方法来知道在C#中的操作是否是原子或没有?还是有经验的任何一般的指导原则或规则?

Is there a systematic way to know whether an operation in C# will be atomic or not? Or are there any general guidelines or rules of thumb?

推荐答案

对于一些更完整的/详细的:

For something more complete/detailed:

读取和写入的32位值类型的原子:这包括以下内在价值(结构)类型:布尔,字符,字节,为sbyte,短USHORT,UINT,浮动。以下几种类型(其中包括)不能保证是原子的:小数,双,长,ULONG

Reads and writes to 32-bit value types are atomic: This includes the following intrinsic value (struct) types: bool, char, byte, sbyte, short ushort, uint, float. the following types (amongst others) are not guaranteed to be atomic: decimal, double, long, ulong.

例如。

int x;
x = 10; // atomic
decimal d;

d = 10m; // not atomic

参考分配也是一个原子操作:

Reference assignment is also an atomic operation:

private String _text;
public void Method(String text)
{
  _text = text; // atomic
}

这篇关于哪些操作是原子在C#中?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆