.NET System.Net.CookieContainer 线程安全吗?

发布于 2024-07-11 01:47:43 字数 421 浏览 7 评论 0原文

  1. .NET 类 System.Net.CookieContainer 线程安全吗? --更新:交钥匙回答--
  2. 有什么方法可以确保异步请求期间修改的变量(即 HttpWebRequest.CookieContainer)的线程安全性?
  3. 是否有任何属性可以突出显示线程安全类? --更新:如果 MSDN 上描述了线程安全性,那么它们可能没有相应的属性 --
  4. 所有 .NET 类都是线程安全的吗? --更新: Marc 回答 --

我问这些问题是因为我在多线程代码的异步请求中使用 CookieContainer。 而且我无法将异步请求放入锁内。 也许我必须像 F# 中那样使用只读“变量”(或不可变类型),对吗?

  1. Is the .NET class System.Net.CookieContainer thread safe? --Update: Turnkey answered--
  2. Is there any way to ensure thread safeness to variables which are modified during asynchronous requests (ie. HttpWebRequest.CookieContainer)?
  3. 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 --
  4. 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 技术交流群。

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

发布评论

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

评论(5

你如我软肋 2024-07-18 01:47:43

不,并非所有 .NET 类都是线程安全的。 事实上,很少有人有必要这样做。 一般来说,静态成员应该是线程安全的,但仅此而已。

不可变/半不可变对象自动是线程安全的(这包括 XslTransform 等) - 并且在少数可变情况(例如线程容器)中您可以期望事物是线程安全的。 MSDN 声明了每个类的线程安全性。

我不期望 cookie 容器是线程安全的,因此您可能必须自己同步它。

(更新)

回复你的第二点; 您到底在考虑哪些变量? 您自己的本地状态变量不会在异步请求期间直接更新,因此您只需在准备请求和处理响应时同步访问即可。 最常见的是,通过 Monitor - 即

lock(syncLock) {
    // prepare request from (synchronized) state
    req.Begin{...}
}

然后在回调中

lock(syncLock) {
    // ...read values from request...
    // ...update local state...
}

,其中 syncLock 只是一个锁对象(可能针对实例持有):

private readonly object syncLock = new object();

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.

lock(syncLock) {
    // prepare request from (synchronized) state
    req.Begin{...}
}

and then in the callback

lock(syncLock) {
    // ...read values from request...
    // ...update local state...
}

Where syncLock is just a lock object (perhaps held against an instance):

private readonly object syncLock = new object();
心是晴朗的。 2024-07-18 01:47:43

来自马口

线程安全< /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.

滿滿的愛 2024-07-18 01:47:43

正如我所见(在 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.

〆一缕阳光ご 2024-07-18 01:47:43

请注意,网页会发送修改后的 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.

冷…雨湿花 2024-07-18 01:47:43

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.

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