XBox 360 上的 XNA 有好的信号量吗?
我正在寻找一种快速有效的 .NET Compact Framework 信号量实现。这里还有另一个关于SO的问题(.NET紧凑框架中的信号量),其中建议使用 P/Invoke,但这在 XBox 360 上运行的 XNA Framework 中是不可能的。
我可以提供我自己的两种实现,但我相信这两种实现都不是最佳的。
使用 AutoResetEvent 的信号量 (pastebin)
托管信号量的一种可能实现是使用 AutoResetEvent。
在这种情况下,当工作可用时,AutoResetEvent 将仅将一个线程转换为“可运行”状态。当操作系统线程调度程序运行该线程时,它将重新打开 AutoResetEvent,使下一个线程进入“可运行”状态。因此,线程将按顺序启动,并且仅在其前驱实际执行之后才启动。
使用 ManualResetEvent 的信号量 (pastebin)
另一种可能的实现是使用 ManualResetEvent。
在这种情况下,当工作可用时,ManualResetEvent 会将所有线程转换为“可运行”状态。操作系统线程调度程序运行的所有线程都会竞争工作项,直到第一个耗尽工作的线程再次重置 ManualResetEvent。换句话说,即使不是所有线程都需要,也可能会在短时间内唤醒所有线程。
有谁知道那里有更好的实施方案或者可以提供改进我的建议吗?
I'm looking for a fast and efficient implementation of a Semaphore for the .NET Compact Framework. There has been another Question here on SO (Semaphores in .NET compact framework) in which it was suggested to use P/Invoke, but this is not possible in the XNA Framework running on the XBox 360.
I can offer two implementations of my own, but both are sub-optimal, I believe.
Semaphore using an AutoResetEvent (pastebin)
One possible implementation of a managed Semaphore would, using an AutoResetEvent.
In this case, when work becomes available, the AutoResetEvent will transitioning only one thread into the 'runnable' state. When the OS thread scheduler runs the thread, it will reopen the AutoResetEvent, bringing the next thread into the 'runnable' state. So threads will be started sequentially, and only after their predecessor actually came to execution.
Semaphore using a ManualResetEvent (pastebin)
Another possible implementation would be using a ManualResetEvent.
In this case, when work becomes available, the ManualResetEvent will transition all threads into the 'runnable' state. All threads the OS thread scheduler runs compete for the work items until the first thread that runs out of work resets the ManualResetEvent again. In other words, possibly all threads will be woken for a short time even if not all threads are required.
Does anyone know of a better implementation out there or can provide suggestions for improving mine?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
编辑:没关系,只需阅读您引用的其他线程即可:信号量类本身不包含在 XBox 的 CF 中吗?
http://msdn.microsoft.com/en-us/ library/system.threading.semaphore.aspx
否则,这是我在 codeproject 上找到的一个实现。我自己没用过,但也许可以作为参考:-)
http://www.codeproject.com/KB/threads/inprocsemaphore.aspx
Edit: nevermind, just read the other thread you referenced: Is the Semaphore class itself not included in the XBox's CF?
http://msdn.microsoft.com/en-us/library/system.threading.semaphore.aspx
Otherwise, here's an implementation I found on codeproject. Haven't used it myself, but perhaps it can serve as a reference :-)
http://www.codeproject.com/KB/threads/inprocsemaphore.aspx
OpenNetCF 有一个 紧凑框架的信号量类。有什么原因导致您无法在 Xbox 上使用 OpenNetCF 库吗?
OpenNetCF has a Semaphore class for the compact framework. Is there any reason you can't use the OpenNetCF libraries on the Xbox?