Tuesday 30 August 2011

Multithreading in RealStudio - avoiding a common pitfall

I've been working on a new project (more details later), which requires a background thread to perform some processing quite frequently. Everything was going fine and all testing was going great, until I started to increase the amount of data in the application.
Suddenly, things seemed to slow down dramatically. I thought this was  due to too much processing in the paint method of a custom control I had created, so I stripped it down as much as possible, removing a lot of the functionality which I would have to replace later. But, none of this seemed to fix the issue.
After a few days, I realised that there was one area, I had not touched: the background thread.

To cut a long (and quite painful) story short, I had treated the thread as I would have in Delphi, Objective-C or any other language I have used in the past. These other languages allow for true multithreading. RealStudio, does not. Rather than having the OS handle the threading, RealStudio uses it's own thread scheduler.

Turns out the fix was this was very simple. In the loop, within the threads execution, I place the following line:

App.YieldToNextThread

before the end of the loop. This allowed the application to perform other actions required in the main thread.

No comments:

Post a Comment