如何使用暂停然后恢复或替代方法在线程之间同步共享数据 [英] How to synchronize shared data between threads by using pause then resume or alternatives

查看:52
本文介绍了如何使用暂停然后恢复或替代方法在线程之间同步共享数据的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在编写一个游戏,其中一个线程 - GameThread - 永远循环,更新我的所有精灵,渲染它们,然后在再次执行之前休眠一段时间.我还有一个定制的事件处理程序,用于处理按键等.

I'm writing a game in which a thread - GameThread - loops forever, updating all my sprites, rendering them, and then sleeping for some time before doing it all again. I also have a custom-made Event handler which deals with key presses etc.

这在大多数情况下都可以正常工作.但是,如果在渲染 GameThread 时抛出事件,我会遇到问题.在极少数情况下,处理事件的处理程序可能会同时更改需要呈现的内容,从而影响 GameThread 呈现的结果.

This all works fine in most cases. However I have a problem if an event is thrown while GameThread is rendering. On rare occasions, the handler that deals with the event may make a concurrent change to what needs to be rendered affecting the results of the GameThread rendering.

为了避免这种情况,我希望事件处理程序立即暂停 GameThread,处理事件,然后恢复 GameThread.

To avoid this, I want the event handler to pause the GameThread immediately, handle the event, then resume GameThread.

  1. suspend()/resume() 方法适合我的需要,但它们已被弃用.但是,在我的情况下,由于死锁的可能性很小,无论如何使用它们是否安全?

  1. The suspend() / resume() methods suit my needs, but they were deprecated. In my case, however, as there is little chance of a deadlock, is it safe to use them regardless?

如果没有,我还有哪些其他没有大量开销的替代方案?

If no, what other alternatives do I have that don't have a huge amount of overhead?

我看到了通过在要暂停的线程中设置标志来请求线程暂停的建议.然而,就我而言,我认为这不是一个可行的选择,因为 GameThread 循环在循环迭代期间可能需要一段时间.在我完成循环之前,我将无法检查标志,然后为时已晚.

I have seen a suggestion of requesting a thread to pause by setting a flag in the Thread to be paused. In my case, however, I don't see that as a viable option since the GameThread loop may take a while during an iteration through the loop. I won't be able to check the flag until I'm done with the loop and by then it is too late.

我需要立即暂停,否则用户会注意到事件处理有延迟.

I need immediate pausing, or else the user will notice a delay in the event handling.

推荐答案

suspend()/resume() 方法适合我的需要,但它们已被弃用.但是,在我的情况下,由于发生僵局的可能性很小,无论如何使用它们是否安全?

The suspend() / resume() methods suit my needs, but they were deprecated. In my case, however, as there is little chance of a deadlock, is it safe to use them regardless?

显然,如果死锁的可能性为零,那么它是安全的.但是有各种意想不到的方法会导致僵局.例如,您可能在初始化一个类时碰巧暂停了一个线程……这将使任何其他试图引用该类的静态字段的线程死锁.(这是 JVM 特定行为的结果.还有其他地方没有指定在引擎盖下进行的锁定/同步.很公平.它不需要......除非你正在考虑使用这些已弃用的方法.)

Obviously, if there is ZERO chance of a deadlock then it is safe. But there are all sorts of unexpected ways to get a deadlock. For instance, you could happen to pause a thread while it is initializing a class ... and that would deadlock any other thread trying to refer to a static field of that class. (This is a consequence of a specified behaviour of the JVM. There are other places where the locking / synchronization that goes on under the hood is not specified. Fair enough. It doesn't need to be ... unless you are contemplating using these deprecated methods.)

所以,现实是很难确定(证明)它是否真的安全.如果你不能确定这一点,那么这是一件有潜在风险的事情.这就是不推荐使用这些方法的原因.

So, the reality is that it is really difficult to determine (prove) if it is actually safe. And if you can't determine this, then it is a potentially risky thing to do. That's WHY the methods are deprecated.

(严格来说,这不是死锁.死锁是指线程永远无法继续执行.在这种情况下,如果您可以恢复暂停的线程,其他线程可以继续执行.)

(Strictly speaking, this is not a deadlock. A deadlock is when the threads can never proceed. In this case, the other threads can proceed if you can resume the paused thread.)

这篇关于如何使用暂停然后恢复或替代方法在线程之间同步共享数据的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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