应用服务器中的多线程

发布于 2024-10-27 07:15:08 字数 283 浏览 0 评论 0原文

“即使您没有在应用程序中明确使用线程,但如果您谈论的是 Web 服务器应用程序,它必然是线程不安全的”。

我只是想清楚地了解这一点。假设我有一个安静的服务(ASP.net;将在图中讨论 asp.net Web 应用程序)。如果对同一个 Web 方法 A 有两个同时请求,则这两个请求将由 IIS wp 的不同线程提供服务。现在,这两个线程在做什么? IE。这两个线程是否在服务类的同一实例上访问 A?

我们如何验证这两个请求是否在服务类的同一个实例上工作,这样实际上由于在 Web 方法中访问实例变量而存在线程不安全问题

"Even though you do not use Threads in an application explicitly, its bound to be thread unsafe if you are talking about web server applications".

I just want to understand this clearly. Assume i have a restful service (ASP.net ; will talk about asp.net web application in picture). If there are two simultaneous requests to the same web method A, both of these are going to be served by different thread of the IIS wp. Now, what are these 2 threads working on? ie. Are these two threads accessing A on the same instance of the service class?

How can we validate the fact that these 2 requests are/ are not working on the same instance of the service class so that there is infact a thread unsafety here because of instance variables being accessed in the web method

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

月寒剑心 2024-11-03 07:15:08

>现在,这两个线程在做什么?

IIS 的简答线程。您需要阐明 IIS 的体系结构 - 将其视为独立的程序。它可以插入(动态加载)许多DLL。其中之一是 ASP.Net 拦截器(它接受类似于 *.aspx、*.ashx 等的扩展名),另一方面,该 DLL 会加载由代码生成的 DLL。因此,为了处理来自客户端的请求,IIS(在一般情况下)启动线程(让我跳过池的讨论)。

>我们如何验证这两个请求是否在服务类的同一个实例上工作这一事实

在上面的场景中,您不需要验证,因为 ASP.Net 将创建您需要的尽可能多的实例。当您使用异步请求功能时会出现此问题(请参阅 http://msdn.microsoft. com/en-us/magazine/cc163725.aspx

<%@ Page Async="true" ... %> 

或手动启动一些线程。
在这两种情况下,您都可以应用非常强大的同步原语集来检测某些资源是否在 2 个或更多线程之间共享。使用原子增量 (Interlocked.Increment/Decrement) 或通过 TryEnter 方法进行监视(有关详细信息,请参阅

> Now, what are these 2 threads working on?

Short answer threads of IIS. You need clarify architecture of IIS - think about this as stand alone program. It can plug (dynamically load) lot of DLLs. One of it is ASP.Net interceptor (it accept extensions looked like *.aspx, *.ashx, ...) In other turn this DLL loads your DLLs produced from your code. So to handle request from client side IIS (in general case) starts thread (let me skip discussion of pooling).

> How can we validate the fact that these 2 requests are/ are not working on the same instance of the service class

In scenario above you don't need to validate, since ASP.Net will create as many instance as you need. The problem appears when you use asynchronous request feature (see http://msdn.microsoft.com/en-us/magazine/cc163725.aspx)

<%@ Page Async="true" ... %> 

Or start some threads manually.
In both cases you could apply really powerful set of synchronization primitives to detect if some resource is shared between 2 or more threads. Using atomic increments (Interlocked.Increment/Decrement) or Monitor with TryEnter methods (for details see http://msdn.microsoft.com/en-us/magazine/cc188793.aspx)

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文