单例类是否在IIS中是线程安全的?

时间:2022-10-22 10:19:31

I have an ASP.NET webpage running under IIS that uses a common assembly that contains a singleton class. Should I implement a locking mechanism on the singleton to make it thread-safe? Or, will any connection to the webserver use the same instance of the singleton?

我有一个在IIS下运行的ASP.NET网页,它使用包含单例类的公共程序集。我应该在单例上实现一个锁定机制,使其具有线程安全性吗?或者,与Web服务器的任何连接是否都使用相同的单例实例?

Hopefully I'm asking this coherently.

希望我能连贯地问这个问题。

3 个解决方案

#1


2  

The usual pattern for .NET singletons creates a single instance per app domain. The usual situation in asp.net is that you have multiple threads running through the same app domain. This means you could very well have multiple threads running code in your singleton at the same time.

.NET单例的常用模式为每个应用程序域创建一个实例。 asp.net中的常见情况是您有多个线程在同一个应用程序域中运行。这意味着你可以让多个线程同时在你的单例中运行代码。

You have to examine the singleton and determine (to the best of your abilities koffreflectorkoff) if it is thread safe or not. If it keeps and modifies shared resources, or if it encapsulates some kind of state which is stored between method calls, be careful.

如果它是线程安全的,你必须检查单例并确定(最好的能力koffreflectorkoff)。如果它保留并修改共享资源,或者它封装了存储在方法调用之间的某种状态,请小心。

You might want to wrap the singleton within a second singleton which uses locks before delegating its calls to the one you don't have control over. That way you could have finer control over locking rather than block every call to the singleton using a single lock. It would also give you the benefit of centralizing the location of the threading code...

您可能希望将单例包装在第二个单例中,该单例使用锁定,然后将其调用委托给您无法控制的单个调用。这样你就可以更好地控制锁定,而不是使用单个锁来阻止对单例的每次调用。它还可以为您提供集中线程代码位置的好处......

#2


2  

If it's a static singleton... no, it's not thread safe. It's shared across all threads that are hitting that instance of the app (so multi-requests will share it).

如果它是一个静态单例...不,它不是线程安全的。它在所有正在击中该应用程序实例的线程*享(因此多请求将共享它)。

However, what you're using it for might not need thread safety necessarily.

但是,您正在使用它可能不一定需要线程安全。

#3


0  

In .NET it is possible to implement a Thread-safe Singleton without using locks.

在.NET中,可以在不使用锁的情况下实现线程安全的Singleton。

#1


2  

The usual pattern for .NET singletons creates a single instance per app domain. The usual situation in asp.net is that you have multiple threads running through the same app domain. This means you could very well have multiple threads running code in your singleton at the same time.

.NET单例的常用模式为每个应用程序域创建一个实例。 asp.net中的常见情况是您有多个线程在同一个应用程序域中运行。这意味着你可以让多个线程同时在你的单例中运行代码。

You have to examine the singleton and determine (to the best of your abilities koffreflectorkoff) if it is thread safe or not. If it keeps and modifies shared resources, or if it encapsulates some kind of state which is stored between method calls, be careful.

如果它是线程安全的,你必须检查单例并确定(最好的能力koffreflectorkoff)。如果它保留并修改共享资源,或者它封装了存储在方法调用之间的某种状态,请小心。

You might want to wrap the singleton within a second singleton which uses locks before delegating its calls to the one you don't have control over. That way you could have finer control over locking rather than block every call to the singleton using a single lock. It would also give you the benefit of centralizing the location of the threading code...

您可能希望将单例包装在第二个单例中,该单例使用锁定,然后将其调用委托给您无法控制的单个调用。这样你就可以更好地控制锁定,而不是使用单个锁来阻止对单例的每次调用。它还可以为您提供集中线程代码位置的好处......

#2


2  

If it's a static singleton... no, it's not thread safe. It's shared across all threads that are hitting that instance of the app (so multi-requests will share it).

如果它是一个静态单例...不,它不是线程安全的。它在所有正在击中该应用程序实例的线程*享(因此多请求将共享它)。

However, what you're using it for might not need thread safety necessarily.

但是,您正在使用它可能不一定需要线程安全。

#3


0  

In .NET it is possible to implement a Thread-safe Singleton without using locks.

在.NET中,可以在不使用锁的情况下实现线程安全的Singleton。