问题标签 [threadstatic]

For questions regarding programming in ECMAScript (JavaScript/JS) and its various dialects/implementations (excluding ActionScript). Note JavaScript is NOT the same as Java! Please include all relevant tags on your question; e.g., [node.js], [jquery], [json], [reactjs], [angular], [ember.js], [vue.js], [typescript], [svelte], etc.

0 投票
1 回答
1772 浏览

c# - 在 C# 中创建线程安全变量

我有一个静态变量,可由多个对象中的多个线程访问。问题是如果我在一个线程中设置值,它不会反映在另一个线程中。为了解决这个问题,我将变量线程设为静态,但一个线程中更改的值仍然会反映在另一个线程中。这就是我声明变量的方式:

关于如何解决问题的任何建议?

0 投票
3 回答
1450 浏览

c# - [ThreadStatic] 的使用是否与异步代码不一致?

我们有一个相当大的现有代码库,用于构建在 ASP.NET 之上的各种 web 服务,并且该代码大量使用访问HttpContext.Current.User(包装为Client.User),我相当确定在内部使用它[ThreadStatic]来为您提供环境范围。

我目前正在研究是否有可能我们开始以 的形式使用更多的异步代码,async/await但我很难找到如何[ThreadStatic]适应这种情况。由于它的大量使用,消除对它的依赖[ThreadStatic]是不可能的。

我的理解是,当 anawait被命中时,代码的执行会停在那里,调用立即返回,并且设置了一个延续以在异步代码返回时继续执行。同时,原始线程可以自由地用于其他事情,例如处理另一个请求。到目前为止我对它的理解。

我无法真正找到明确的答案是HttpContext.Current.Userawait.

所以基本上:

Debug.Assert能保证成功吗?

如果另一个请求由与待处理的线程相同的线程Task.Delay处理,则该请求将设置一个不同的HttpContext.Current.User,那么当调用 continuation 时,以前的状态是否以某种方式存储和恢复?


我可以想象发生的事情是,在幕后,[ThreadStatic]状态作为某种字典保存在线程本身上,并且当一个线程在返回后返回线程池时,await该字典被保存在某个地方安全并返回到线程时它执行延续(或者在一个线程上,我不确定它是否一定是处理延续的同一个线程),可能是在屁股上鼓励拍拍和“去拿他们的男孩!”,但最后部分可能只是我的想象。

这有点准确吗?

更新:我试图组织一个小测试来尝试这个。到目前为止,它似乎有效,并且对于数百个请求中的任何一个,断言都没有失败。任何人都可以验证测试是否有意义?

https://gist.github.com/anonymous/72d0d6f5ac04babab7b6

0 投票
1 回答
1068 浏览

c# - CRM 2011 组织服务处置

我们正在创建一个与 CRM 2011 对话的 ASP.NET MVC 应用程序。我们正在使用 Xrm.Client.Services.OrganizationService。为此,我们有一个单例模式。

在负载下,我们看到一个 ObjectDisposedException(无法访问已处置的对象)。我认为单身人士不是去这里的方式。

我正在考虑一个 [ThreadStatic] 单例,或者一个服务池,或者任何其他可能有帮助的东西。有人知道可以建立的 CRM 连接数量有任何限制吗?有人体验过disposed service吗?有人告诉我,一次连接到 CRM 的组织服务太多也会有问题,所以这就是为什么我没有为每次访问 CRM 创建一个新服务。

感谢您的任何帮助,您可以提供!如果需要更多说明,请询问。

0 投票
2 回答
1028 浏览

c# - 线程与任务中的 ThreadStatic 属性

当我使用 ThreadStatic 属性探索线程和任务时,我遇到了一些奇怪的事情。我相信这可能非常特定于线程和任务。考虑下面的代码片段:

这给出了输出:

这绝对没问题,因为 ThreadStatic 变量只能初始化一次,所以第二行显示为 0。

但是,请考虑以下情况:

这一行给了我输出:

我跨越了多少线程,我真的看不到“范围”值被初始化并显示为 10。这里初始化的范围变量在哪里以及为什么在初始化静态变量时线程和任务之间存在区别?

我在这里缺少一些基本的东西吗?提前致谢。

0 投票
1 回答
269 浏览

c# - 为什么在 Threadlocal 委托中初始化 threadstatic 属性不会为第一个线程初始化它?

我已经创建了 3 个线程,并且所有线程都对 thread1 之外的 threadlocal 属性进行了增量操作。我还在 threadlocal 委托中通过值 11 初始化 threadstatic 属性。这里我总是在第一个线程中获得 num = 0 的值。为什么这样?

0 投票
1 回答
1640 浏览

asp.net - 异步 ASP.NET Web API 中的 ThreadStatic

是否有可能在单个请求中使用线程静态变量?当前代码使用线程静态变量进行日志记录,现在我们想使用异步控制器方法(带有异步和等待模式),这会导致问题,因为当打开新线程时变量为空。

0 投票
1 回答
6016 浏览

c# - What's the effect of AsyncLocal in non async/await code?

I'm working on a very large and old code base of a desktop winform application. In this code base there are lots of operations performed in background threads, mainly using BackgroundWorker.

A common pattern in this code base, is to hide complexity by binding artifacts to the thread being executed. For instance, the database connection and transaction are stored in [ThreadStatic] fields.

I'm trying to change this, and start using async/await code, and benefit from running the task in any thread of the pool, and allowing a task to continue executing in any other thread by using ConfigureAwait(false). I know that [ThreadStatic] doesn't play nice with async/await, and I've read several answers over here suggesting to use AsyncLocal<T> instead.

Given that I'm working on a large code base, as mentioned before, I'm unable to switch to async/await everywhere in a single shot, and I must do this changes gradually. So the code that before had [ThreadStatic] will change to AsyncLocal<T>, but large portions of the code will continue using BackgroundWorker and won't hit a single async/await line of code.

Question
Will this work? I need to be able to define some kind of context flow that will work with my new async/await code, and also keep working with my old non async code which relied on [ThreadStatic] keeping every thread stuff independent from each other.

If I'm totally wrong and going down the wrong path, suggestions are very welcomed.

0 投票
2 回答
970 浏览

c# - ThreadStatic 的 C# 单例模式设计

我想弄清楚单例模式设计。我想为我的单例类中的每个线程创建单独的实例。所以我在下面提供了两个设计。

这是工作

它不起作用(抛出 NullReferenceException 并且未创建实例。)

我真的很想知道为什么不为第二个设计创建一个实例。有人可以解释一下吗?

0 投票
1 回答
533 浏览

c# - CallContext 正在向前传递之前设置的数据

我有这种情况,我看到线程的 CallContext 在后续调用中向前携带数据。

考虑我有一个简单的 API,当被查询时,它将使用以下方法将一个数据条目设置到 CallContext 中:

当我在一些 API 查询后看到日志时,我看到了类似的日志。

| 线程 | 日志 |
| 237 | 数据 23 |
| 145 | 数据 19 |
| 第872章 数据 78 |
| 237 | 数据 23 |

我担心的是为什么 ID 为 237 的线程会提取旧数据?即23
我确定控件没有进入LogicalSetData 代码块,因为它已经有数据。

我不确定为什么会这样?谁能帮我这个?

该服务是 WCF 数据服务。正在从邮递员 REST 客户端进行呼叫。

0 投票
1 回答
150 浏览

c# - 从 .NET 探查器访问 ThreadStatic 字段

stackoverflow中的这个问题询问[ThreadStatic]是 如何实现的:ThreadStatic属性是如何工作的?

有人建议将其视为 Thread 对象的扩展。我不确定这是否意味着它基于 win32 TLS。

我的问题是我能否以某种方式从 .NET 探查器代码中的当前线程访问 [ThreadStatic] 的值?也就是说,在本机代码中。

例如,如果我可以使用 win32 线程 id 找到内存中所有线程静态字段所在的区域,并找到我需要检索的特定字段。

谢谢