什么陷阱与任务和垃圾收集存在吗? [英] What gotchas exist with Tasks and Garbage Collection?

查看:151
本文介绍了什么陷阱与任务和垃圾收集存在吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在没有开发人员需要与垃圾收集的影响而使用有关的任务派生的API和类时并行库?

When does a developer need to be concerned with the effects of garbage collection when using APIs and classes derived from the Task Parallel Library?

<一个href="http://stackoverflow.com/questions/2782802/can-net-task-instances-go-out-of-scope-during-run">Can运行期间.NET任务实例走出去的范围?,似乎给安全,你不用担心保存的任务范围之感。然而,问题似乎仅限于线程池正在运行的任务,他们是那么的线程池。但是,如果我的理解这个MSDN博客帖子正确,从该通知,这样的问题是不是普遍适用的,因为从任务 TaskCompletionSource 不是同样

Can .NET Task instances go out of scope during run?, would seem to give a sense of security that you do not have to worry about keeping tasks in scope. However the question seems limited to Tasks running on the ThreadPool where they are then rooted by the ThreadPool. However, if I understand this MSDN blog post correctly, the advice from that SO question is not be generally applicable because Tasks from TaskCompletionSource are not similarly rooted.

是直接用关切的 TaskCompletionSource 的唯一一次?
然而,耗时一个API,当你不知道那里的任务是从哪里来的。你再不用担心存储的参考延续的情况下,提供了工作来自一个 TaskCompletionSource 或其它非-rooted源?

Are direct use of TaskCompletionSource the only time of concern?
However, when consuming an API you do not know where the Task came from. Do you then need to worry about storing references to continuations in case the provided Task came from a TaskCompletionSource or some other non-rooted source?

这似乎从需要考虑的任务是否是植根还是没有得到不便和复杂迅速(根植异步I / O任务?)。我苦苦寻找多的关于主题的信息,但一个人气不足图书馆,我觉得我不应该必须阅读反编译源$ C ​​$ C,以确定是否我需要担心的比赛条件与垃圾回收器,所以我的数字我一定是丢失或误解的东西。

This seems to get inconvenient and complex quickly from needing to consider whether the Task is rooted or not (are Async I/O Tasks rooted?). I am struggling to find much for information on topic but it a popular enough library I feel I should not need to be reading decompiled source code to determine if I need to worry about race-conditions with the garbage collector, so I figure I must be missing or misunderstanding something.

推荐答案

当你有未完成的 TaskCompletionSource ,然后总有两个选项:

When you have uncompleted TaskCompletionSource, then there are always two options:

  1. 东西可能完成这项TCS的未来。这意味着,一些持有引用TCS,这意味着它不能得到GCed。

  1. Something might complete that TCS in the future. That means that that something holds a reference to the TCS, which means it can't get GCed.

普通规则仍然适用的东西,所以你可能需要担心保留了根。

Normal rules still apply to that something, so you might need to worry about keeping that rooted.

任何事都不能完整的TCS。这意味着,TCS和任务可能会得到GCed很快,但工作没有风险没有做(因为没有工作)。

Nothing will ever complete that TCS. That means the TCS and its Task will likely get GCed soon, but there is no risk of work not being done (because there is no work).

这篇关于什么陷阱与任务和垃圾收集存在吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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