通过 C++ 互操作或其他方式进行 C# 第一类延续? [英] C# first class continuation via C++ interop or some other way?

查看:19
本文介绍了通过 C++ 互操作或其他方式进行 C# 第一类延续?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我们有一个非常高性能的多任务处理、近乎实时的 C# 应用程序.这种性能主要是通过使用自制调度程序在内部实施协作多任务来实现的.这通常称为微线程.在这个系统中,所有任务都通过队列与其他任务进行通信.

We have a very high performance multitasking, near real-time C# application. This performance was achieved primarily by implementing cooperative multitasking in-house with a home grown scheduler. This is often called micro-threads. In this system all the tasks communicate with other tasks via queues.

我们遇到的具体问题似乎只能通过 C# 不支持的第一类延续来解决.

The specific problem that we have seems to only be solvable via first class continuations which C# does not support.

特别是在处理队列的两种情况下会出现问题.每当任何特定任务在将项目放入队列之前执行一些工作时.队列满了怎么办?

Specifically the problem arises in 2 cases dealing with queues. Whenever any particular task performs some work before placing an item on a queue. What if the queue is full?

相反,一个不同的任务可能会做一些工作,然后需要从队列中取出一个项目.如果该队列为空怎么办?

Conversely, a different task may do some work and then need to take an item off of a queue. What if that queue is empty?

我们已经在 90% 的情况下解决了这个问题,方法是将队列链接到任务以避免任务在其任何出站队列已满或入站队列为空时被调用.

We have solved this in 90% of the cases by linking queues to tasks to avoid tasks getting invoked if any of their outbound queues are full or inbound queue is empty.

此外,某些任务已转换为状态机,因此它们可以在队列满/空时进行处理,并且无需等待即可继续.

Furthermore certain tasks were converted into state machines so they can handle if a queue is full/empty and continue without waiting.

真正的问题出现在一些极端情况下,其中任何一种解决方案都不切实际.这种情况下的想法是在该点保存堆栈状态并切换到不同的任务,以便它可以完成工作,然后在能够继续时重试等待的任务.

The real problem arises in a few edge cases where it is impractical to do either of those solutions. The idea in that scenario would be to save the stack state at the point and switch to a different task so that it can do the work and subsequently retry the waiting task whenever it is able to continue.

过去,我们试图让等待任务回调到调度中(递归),以允许其他任务稍后重试等待任务.然而,这导致了太多的死锁"情况.

In the past, we attempted to have the waiting task call back into the schedule (recursively) to allow the other tasks to and later retry the waiting task. However, that led to too many "deadlock" situations.

在某个地方有一个自定义 CLR 主机的示例,它使 .NET 线程实际上作为纤程"运行,这实质上允许在线程之间切换堆栈状态.但现在我似乎找不到任何示例代码.此外,似乎需要一些相当复杂的事情才能让它正确.

There was an example somewhere of a custom CLR host to make the .NET threads actually operate as "fibers" which essentially allows switching stack state between threads. But now I can't seem to find any sample code for that. Plus it seems that will take some significant complexity to get it right.

有没有人有其他的创意想法如何在任务之间高效切换并避免上述问题?

Does anyone have any other creative ideas how to switch between tasks efficiently and avoid the above problems?

是否有任何其他 CLR 主机提供此功能(商业或其他方式)?是否有任何附加的原生库可以为 C# 提供某种形式的延续?

Are there any other CLR hosts that offer this, commercial or otherwise? Is there any add-on native library that can offer some form of continuations for C#?

推荐答案

实际上,我们决定了一个方向.我们使用带有消息传递的观察者模式.我们构建了一个本地库来处理类似于 Erlang 进程的代理"之间的所有通信.稍后我们将考虑使用 AppDomains 来更好地将代理彼此分开.设计思想借鉴了 Erlang 编程语言,具有极其可靠的多核和分布式处理.

Actually, we decided on a direction to go with this. We're using the Observer pattern with Message Passsing. We built a home grown library to handle all communication between "Agents" which are similar to an Erlang process. Later we will consider using AppDomains to even better separate Agents from each other. Design ideas were borrowed from the Erlang programming language which has extremely reliable mult-core and distributed processing.

这篇关于通过 C++ 互操作或其他方式进行 C# 第一类延续?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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