数组和线程安全访问 [英] Array and Safe Access by Threads

查看:165
本文介绍了数组和线程安全访问的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如果我有一个可以/会被多个线程在任何时间点访问数组,究竟是什么导致其非线程安全的,这将是采取措施,确保该数组将是线程的步骤在大多数情况下安全吗?

If I have an array that can/will be accessed by multiple threads at any given point in time, what exactly causes it to be non-thread safe, and what would be the steps taken to ensure that the array would be thread safe in most situations?

我已经广泛地环顾四周,在互联网上发现了一点关于这个问题的任何信息,一切似乎是特定的场景(例如被这阵,正在访问像这样通过这两个线程的线程安全的,并在和上)。我真的希望有人能回答任何问题我布置在顶部,或者如果有人能够向好的文档指向解释说,项目。

I have looked extensively around on the internet and have found little to no information on this subject, everything seems to be specific scenarios (e.g. is this array, that is being accessed like this by these two threads thread-safe, and on, and on). I would really like of someone could either answer the questions I laid out at the top, or if someone could point towards a good document explaining said items.

编辑:
在MSDN上四处寻找后,我发现ArrayList类。当您使用同步方法,返回给定列表中的线程安全包装。当列表设置数据(即list1的[someNumber] = anotherNumber;)?不包装自动采取锁定列表的照顾,或者你还需要将其锁定

After looking around on MSDN, I found the ArrayList class. When you use the synchronize method, it returns a thread-safe wrapper for a given list. When setting data in the list (i.e. list1[someNumber] = anotherNumber;) does the wrapper automatically take care of locking the list, or do you still need to lock it?

推荐答案

当两个线程访问相同的资源(例如,而不是本地副本,但实际上是同一资源的相同副本),许多事情都可能发生。在最明显的情况下,如果线程#1访问资源和线程#2的改变它中期看,一些未predictable行为可能发生。即使有一些作为整数一样简单,你可以有出现错误的逻辑,所以试着想象,可导致不恰当地使用更复杂的东西,像是一个声明为静态的数据库访问类的恐怖。

When two threads are accessing the exact same resource (e.g., not local copies, but actually the same copy of the same resource), a number of things can happen. In the most obvious scenario, if Thread #1 is accessing a resource and Thread #2 changes it mid-read, some unpredictable behavior can happen. Even with something as simple as an integer, you could have logic errors arise, so try to imagine the horrors that can result from improperly using something more complicated, like a database access class that's declared as static.

的处理这个问题的传统方法是将一个锁在敏感的资源,以便只有一个线程可以一次使用它。因此,在上述例子中,线程#1将请求锁的资源和被授予它,然后进入在读取它需要什么来阅读。线程#2将沿着读中旬来请求锁的资源,但被拒绝,并告知要等待,因为线程#1在使用它。当线程#1结束时,会释放锁,它的线程#2进行确定。

The classical way of handling this problem is to put a lock on the sensitive resources so only one thread can use it at a time. So in the above example, Thread #1 would request a lock to a resource and be granted it, then go in to read what it needs to read. Thread #2 would come along mid-read and request a lock to the resource, but be denied and told to wait because Thread #1 is using it. When Thread #1 finishes, it releases the lock and it's OK for Thread #2 to proceed.

有其他情况,但是这示出的最基本的问题和解决方案之一。在C#中,您可以:

There are other situations, but this illustrates one of the most basic problems and solutions. In C#, you may:

1)使用由框架(如蝎子王子的链接进行管理的具体上锁.NET对象到SynchronizedCollection

1) Use specific .NET objects that are managed as lockable by the framework (like Scorpion-Prince's link to SynchronizedCollection)

2)使用<一个href=\"http://msdn.microsoft.com/en-us/library/system.runtime.compilerservices.methodimploptions.aspx\">[MethodImpl(MethodImplOptions.Synchronized)]以决定了只能由一个线程在同一时间使用,做一些危险的特定方法

2) Use [MethodImpl(MethodImplOptions.Synchronized)] to dictate that a specific method that does something dangerous should only be used by one thread at a time

3)使用 lock语句以特定隔离正在做一些有潜在危险的code行

3) Use the lock statement to isolate specific lines of code that are doing something potentially dangerous

什么方法是最好是真的取决于你的情况。

What approach is best is really up to your situation.

这篇关于数组和线程安全访问的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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