简体   繁体   English

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

[英]Is .NET System.Net.CookieContainer thread safe?

  1. Is the .NET class System.Net.CookieContainer thread safe? .NET类System.Net.CookieContainer线程是否安全? -- Update: Turnkey answered-- - 更新:交钥匙回答 -
  2. Is there any way to ensure thread safeness to variables which are modified during asynchronous requests (ie. HttpWebRequest.CookieContainer)? 有没有办法确保在异步请求期间修改的变量的线程安全性(即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 -- - 更新:如果在MSDN上描述了线程安全性,那么它们可能没有这个属性 -
  4. Are all .NET classes thread safe? 所有.NET类都是安全的吗? -- Update: Marc answered-- - 更新: Marc回答 -

I ask these questions because I use the CookieContainer in asynchronous requests in a multithreaded code. 我问这些问题是因为我在多线程代码中的异步请求中使用CookieContainer。 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? 也许我必须像F#一样使用只读“变量”(或不可变类型),对吗?

No, not all .NET classes are thread safe. 不,并非所有.NET类都是线程安全的。 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. 不可变/半不可变对象是自动线程安全的(这包括诸如XslTransform之类的东西) - 并且有一些可变的情况(例如线程容器),你可以期望事物是线程安全的。 MSDN states thread-safety for each class. MSDN声明每个类的线程安全性。

I would have no expectation for a cookie-container to be thread-safe, so you will probably have to synchronize this yourself. 我不希望cookie容器是线程安全的,所以你可能需要自己同步。

(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 - ie 最常见的是,通过Monitor - 即

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): 其中syncLock只是一个锁对象(可能是针对一个实例):

private readonly object syncLock = new object();

From the horses mouth : 马口

Thread Safety 线程安全

Any public static (Shared in Visual Basic) members of this type are thread safe. 此类型的任何公共静态(在Visual Basic中为Shared)成员都是线程安全的。 Any instance members are not guaranteed to be thread safe. 任何实例成员都不保证是线程安全的。

Edit: 编辑:

You could put a lock around actions that modify the instance members. 您可以锁定修改实例成员的操作。

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. 正如我所看到的(在Reflector的帮助下),CookieContainer在内部使用锁来访问其成员,因此尽管有文档,它应该是线程安全的。

By the way, it has no public static members at all. 顺便说一句,它根本没有公共静态成员 So it seems to me that the documentation provides just a standard notice. 因此在我看来,文档只提供标准通知。

Just a note, a web page sends a modifed cookie list as part of its HTTP reply. 只是一个注释,一个网页发送一个修改过的cookie列表作为其HTTP回复的一部分。 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. 在发送回复后修改CookieContainer将无法完成任何操作 - 您只需修改不再存在的页面请求的cookie集合。

All static classes in the .NET framework are guaranteed by Microsoft to be thread safe. Microsoft保证.NET框架中的所有静态类都是线程安全的。

You can verify this by using Reflector. 您可以使用Reflector验证这一点。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM