1

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?

4

2 回答 2

8

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.

于 2009-02-02T17:15:37.620 回答
5

您所描述的是定界的延续。可悲的是,CLR 不支持这一点,并且无法通过我知道的任何技巧在托管代码(甚至混合代码)中实现。

目前,我需要 IronScheme 中的这个功能。有一些方法可以在 CLR 上创建这种功能,方法是编写一个进行 CPS 转换的编译器,但这会导致许多互操作性问题(您不能直接调用 .NET 函数等)。

我在 IronScheme 中对 CPS 转换做了一些实验,但是我当前的编译器并不真正适合这个,而且执行速度很慢。

于 2009-02-15T20:54:17.697 回答