是一个bool读/写原子在C# [英] Is a bool read/write atomic in C#
本文介绍了是一个bool读/写原子在C#的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
正在访问C#中的 bool 字段原子?特别是,我需要锁定:
Is accessing a bool field atomic in C#? In particular, do I need to put a lock around:
class Foo
{
private bool _bar;
//... in some function on any thread (or many threads)
_bar = true;
//... same for a read
if (_bar) { ... }
}
推荐答案
是的。
以下数据类型的读取和写入是原子的:bool,char,byte,sbyte,short,ushort,uint,int,float和引用类型。
Reads and writes of the following data types are atomic: bool, char, byte, sbyte, short, ushort, uint, int, float, and reference types.
< .71).aspxrel =nofollow noreferrer> C#语言规范。
编辑:可能还值得了解 volatile 关键字。
It's probably also worthwhile understanding the volatile keyword.
这篇关于是一个bool读/写原子在C#的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文