TaskLocal< T>类? [英] TaskLocal<T> Class?

查看:186
本文介绍了TaskLocal< T>类?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我们打算TPL加入到我们的方法是长时间运行,并且可以被分成并行子过程的特定部分。我们是在这一点上会碰到的核心问题是,我们目前的code是依赖于的EntityFramework 6.1的DbContext这不是线程安全的。我花了一些时间挖掘到第三方物流及其InnerWorkings的。它不会出现,我们可以使用ThreadLocal的,作为一个任务可以/将运行在其上的多个线程在其整个生命周期(如果我理解正确)和执行的任务将最终得到多个ThreadLocal的实例进行交互。

We are looking to add TPL to a particular portion of our processes that are long-running and can be split into concurrent sub-processes. The core issue that we are running into at this point is that our current code is dependent on an EntityFramework 6.1 DbContext which is not thread-safe. I have spent some time digging into TPL and its innerworkings. It doesn't appear that we can use ThreadLocal, as a Task can/will have multiple threads running on it throughout its lifetime (if I understand correctly) and the executing task will ultimately be interacting with multiple ThreadLocal instances.

在下以为我身边这是一个可以存储的DbContext整个任务的生命周期(从而清晰地从运行同样的code其它并发任务分离TaskLocal对象 - 每一个都有自己相应的TaskLocal)。

The next thought I had around this is a TaskLocal object that could store the DbContext throughout the lifetime of the task (thereby cleanly separating it from other concurrent Tasks that are running this same code -- each with their own corresponding TaskLocal).

有提到这样的事情在<一个href="http://stackoverflow.com/questions/7961233/how-to-manage-thread-local-storage-tls-when-using-tpl/31545882#31545882">this线程但它不提供任何code或链接到code。

There is mention of something like this in this thread but it does not offer any code or link to code.

这可能吗?而且,我是太平人寿的理解迄今是否正确?

Is this possible? And also, is my understanding of TPL thus far correct?

推荐答案

其实,.NET已经有一个:的 AsyncLocal&LT; T&GT;
。 看起来这是你想要的。

Actually, .NET already has one: AsyncLocal<T> class.
Looks like this is what you want.

这篇关于TaskLocal&LT; T&GT;类?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆