Cocos2d 应用程序的线程同步

发布于 2025-01-06 10:46:47 字数 197 浏览 0 评论 0原文

在我的 cocos2d 游戏中,我有一些必须销毁的球,并且有 2 个线程彼此并发,第一个线程将球添加到 NSMutablearray,第二个线程迭代此数组并为每个球调用释放方法,i已将每个数组操作放入 @synchronized(array) 的同步块中,但它不受影响,并且每次在同步块中应用程序都会抛出异常 __NSArrayM 在枚举时发生突变: 也许还有其他方法来同步线程?

In my cocos2d game i have a some balls which must be destroyed, and there are 2 threads which are concurrent with each other, first thread add balls to the NSMutablearray, and second thread iterate through this array and calls release method for each ball,i have put every operation with array in synchronized block with @synchronized(array) but its not affect and every time in synchronized block application throws an exception __NSArrayM was mutated while being enumerated:
maybe there is other way to synchronize threads?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

梦旅人picnic 2025-01-13 10:46:47

由于您从一个线程添加对象并使用另一个线程迭代同一数组,因此对这部分代码进行多线程似乎毫无意义。

原因是您 在迭代数组时无法修改数组,无论您是在同一线程还是多个线程中执行此操作。

通过使用两个数组,每个线程一个,并且每个线程执行相同的任务,您很可能会获得更好的结果:两个数组都在添加对象,然后都迭代其对象的一半。如何分割对象取决于您,它可以基于屏幕坐标(屏幕分割)或其他一些条件(即每个线程处理的对象的平衡数量)。

Since you're adding objects from one thread and iterating over the same array with another thread, it seems rather pointless to multithread this part of your code.

The reason is that you can not modify an array while iterating over it, regardless of whether you do it from within the same thread or multiple threads.

You will most likely get better results by using two arrays, one for each thread, and each thread perfoming the same tasks: both are adding objects, then both iterate over their half half of the objects. How you split the objects is up to you, it could be based on screen coordinates (screen split) or some other condition (ie balance number of objects processed by each thread).

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文