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

查看:48
本文介绍了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 位值类型是原子的:这包括以下固有值(结构)类型:bool、char、byte、sbyte、short、ushort、int、uint、float.以下类型(以及其他类型)不能保证是原子的:decimal、double、long、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, int, 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天全站免登陆