Multi-threading in RGSS

Post date: Feb 28, 2010 1:44:25 PM

Here are a few thoughts on multi-threading in RGSS. They may very well be applicable to RGSS2, but on the other hand there might be differences.

I am assuming that you have knowledge on the multi-threading in general.

Single Core

It doesn't matter how many threads you make, only a single core on a single cpu can be utilized in RGSS.

I believe this is a short-coming of the Ruby interpreter itself although I am not completely sure.

It does seem like the Audio runs in a thread outside the Ruby environment and thus may be able to use another core to process the audio. (I am not completely sure about this)

In conclusion you should not expect any speed improvements by using more threads.

Graphic.update

As you can derive from the name this method updates the graphics. It also tries to keep the frame-rate stable. This means that it halts until enough time has passed.

The problem is that Graphic.update does not only halt the thread calling the method. It halts all threads. You cannot stop calling Graphics.update since you will get a Script Hanging error after 8-10 seconds.

You can basically expect the performance of the other threads to decrease significantly.

Sprites

Sprites appear not to be thread-safe. If you create sprites in one thread while another thread calls Graphics.update you may very well face a race condition which causes the game to crash.

It may perfectly be possible to safe-guard the Graphics.update call (by aliasing and making critical regions) so that the internal Graphics.update will never be called.

I suggest that you try to have all the sprite creation in the same thread as the Graphics.update. That way I don't think any nasty surprises will await you and allow normal thread-safety measures.

Conclusion

Performance-wise there is no real benefit in using more than 1 thread. In fact it will probably decrease the performance. Therefore only use more than one thread if alternatives makes the scripting much more complicated.

An example of where multi-threading could help is with a loading script or progress script where one thread takes care of handling sprites and updating Graphics.update as little as possible while the other thread does its loading or fancy (slow) algorithms.