【发布时间】:2011-08-13 05:40:48
【问题描述】:
我对 C# 很陌生,所以请多多包涵。我对线程安全有点困惑。什么时候是线程安全的,什么时候不是?
读取(只是从之前初始化的内容中读取)是否总是线程安全的?
//EXAMPLE
RSACryptoServiceProvider rsa = new RSACrytoServiceProvider();
rsa.FromXmlString(xmlString);
//Is this thread safe if xml String is predifined
//and this code can be called from multiple threads?
访问数组或列表中的对象是否总是线程安全的(如果您使用 for 循环进行枚举)?
//EXAMPLE (a is local to thread, array and list are global)
int a = 0;
for(int i=0; i<10; i++)
{
a += array[i];
a -= list.ElementAt(i);
}
枚举总是/永远是线程安全的吗?
//EXAMPLE
foreach(Object o in list)
{
//do something with o
}
写入和读取特定字段是否会导致读取损坏(字段的一半已更改,而另一半仍保持不变)?
感谢您的所有回答和时间。
编辑:我的意思是如果所有线程都只是读取和使用(而不是写入或更改)对象。 (除了最后一个问题,很明显我的意思是线程是否既读又写)。因为我不知道普通访问或枚举是否是线程安全的。
【问题讨论】:
标签: c# arrays string thread-safety enumeration