在 .NET 中,调用堆栈是否与线程密不可分?
是否有可能在函数执行过程中设置一个指向当前堆栈的指针(稍后将被拾取),然后释放当前线程(不展开调用堆栈)并将其返回给线程池? 然后,让另一个线程从该线程停止的地方继续吗? 我知道这意味着调用该函数的人不会知道当前线程上下文已更改,并且可能需要编写一些自定义 IL 代码来执行类似的操作,但是有什么方法可以做到这一点吗?
Is it at all possible in the middle of a function execution to set a pointer to the current stack (to be picked up later) and then release the current thread (without unwinding the call stack) and give it back to the thread pool? And then, have another thread pick up where that thread left off? I know it would mean that someone calling into the function would not know that the current thread context would have changed, and it would probably involve writing some custom IL code to do something like this, but is there ANY way to do this?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
不,堆栈是线程状态的一部分。 您可以使用异步工作流程来执行此类操作(CCR 使这变得更容易),但您不能只是将线程放回线程池。
您可以编写一个线程池来执行此操作,但在我看来,这将是一个坏主意 - 在我看来,这将是相当于调用 Application.DoEvents 的线程池。
No, a stack is part of the state of a thread. You can use asynchronous workflows to do this sort of thing (and the CCR makes this easier) but you can't just give the thread back to the thread pool.
You could write a thread pool which did do this, but it would be a bad idea IMO - it would be the thread pool equivalent of calling Application.DoEvents IMO.
您所描述的是定界延续。 遗憾的是,CLR 对此不支持,并且无法使用我所知道的任何技巧在托管代码(甚至混合代码)中实现。
目前,我需要 IronScheme 中的此功能。 有多种方法可以通过编写执行 CPS 转换的编译器来在 CLR 上创建此类功能,但这会导致许多互操作性问题(无法直接调用 .NET 函数等)。
我在 IronScheme 中对 CPS 转换进行了一些实验,但我当前的编译器并不真正适合于此,并且执行速度相当慢。
What you are describing is delimited continuations. Sadly, the CLR has no support for this, and cannot be implemented in managed code (not even mixed code) with any tricks I know of.
Currently, I need this functionality in IronScheme. There are ways to create this kind of functionality on the CLR, by writing a compiler that does CPS conversion, but that causes a host of interoperability issues (you cannot call .NET functions directly, etc).
I have made some experiments with CPS conversion in IronScheme, but my current compiler is not really suited for this, and performs rather slow.