.NET System.Net.CookieContainer 线程安全吗?
- .NET 类 System.Net.CookieContainer 线程安全吗? --更新:交钥匙回答--
- 有什么方法可以确保异步请求期间修改的变量(即 HttpWebRequest.CookieContainer)的线程安全性?
- 是否有任何属性可以突出显示线程安全类? --更新:如果 MSDN 上描述了线程安全性,那么它们可能没有相应的属性 --
- 所有 .NET 类都是线程安全的吗? --更新: Marc 回答 --
我问这些问题是因为我在多线程代码的异步请求中使用 CookieContainer。 而且我无法将异步请求放入锁内。 也许我必须像 F# 中那样使用只读“变量”(或不可变类型),对吗?
- Is the .NET class System.Net.CookieContainer thread safe? --Update: Turnkey answered--
- Is there any way to ensure thread safeness to variables which are modified during asynchronous requests (ie. HttpWebRequest.CookieContainer)?
- Is there any attribute to highlight thread safe classes? --Update: If thread-safeness is described on MSDN then probably they don't have an attribute for this --
- Are all .NET classes thread safe? --Update: Marc answered--
I ask these questions because I use the CookieContainer in asynchronous requests in a multithreaded code. And I can't put an asynchrounous request inside a lock. Maybe I'll have to use readonly "variables" (or immutable types) like in F#, right?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
不,并非所有 .NET 类都是线程安全的。 事实上,很少有人有必要这样做。 一般来说,静态成员应该是线程安全的,但仅此而已。
不可变/半不可变对象自动是线程安全的(这包括 XslTransform 等) - 并且在少数可变情况(例如线程容器)中您可以期望事物是线程安全的。 MSDN 声明了每个类的线程安全性。
我不期望 cookie 容器是线程安全的,因此您可能必须自己同步它。
(更新)
回复你的第二点; 您到底在考虑哪些变量? 您自己的本地状态变量不会在异步请求期间直接更新,因此您只需在准备请求和处理响应时同步访问即可。 最常见的是,通过
Monitor
- 即然后在回调中
,其中
syncLock
只是一个锁对象(可能针对实例持有):No, not all .NET classes are thread safe. In fact, very few have a need to be. In general, static members should be thread-safe, but that is about it.
Immutable / semi-immutable objects are automatically thread safe (this includes things like XslTransform etc) - and there are a mutable few cases (such as threaded containers) where you can expect things to be thread safe. MSDN states thread-safety for each class.
I would have no expectation for a cookie-container to be thread-safe, so you will probably have to synchronize this yourself.
(updated)
Re your second point; exactly which variables are you thinking of? Your own local state variables won't be directly updated during the async request, so it simply falls to you to synchronize access when preparing requests are when processing responses. Most commonly, via a
Monitor
- i.e.and then in the callback
Where
syncLock
is just a lock object (perhaps held against an instance):来自马口:
线程安全< /strong>
此类型的任何公共静态(在 Visual Basic 中为共享)成员都是线程安全的。 不保证任何实例成员都是线程安全的。
编辑:
您可以锁定修改实例成员的操作。
From the horses mouth:
Thread Safety
Any public static (Shared in Visual Basic) members of this type are thread safe. Any instance members are not guaranteed to be thread safe.
Edit:
You could put a lock around actions that modify the instance members.
正如我所见(在 Reflector 的帮助下),CookieContainer 内部使用锁来访问其成员,因此尽管有文档,它应该是线程安全的。
顺便说一句,它根本没有公共静态成员。 所以在我看来,该文档只是提供了一个标准通知。
As I see (with a help of the Reflector), CookieContainer internally uses locks to access its members, so it should be thread safe in spite of the documentation.
By the way, it has no public static members at all. So it seems to me that the documentation provides just a standard notice.
请注意,网页会发送修改后的 cookie 列表作为其 HTTP 回复的一部分。 发送回复后修改 CookieContainer 不会完成任何事情——您只会修改不再存在的页面请求的 cookie 集合。
Just a note, a web page sends a modifed cookie list as part of its HTTP reply. Modifying the CookieContainer after the reply has been send won't accomplish anything-- you'll just modify the cookie collection of a page request that no longer exists.
Microsoft 保证.NET 框架中的所有静态类都是线程安全的。
您可以使用 Reflector 来验证这一点。
All static classes in the .NET framework are guaranteed by Microsoft to be thread safe.
You can verify this by using Reflector.